Quantcast
Channel: Questions in topic: "clone"
Viewing all articles
Browse latest Browse all 508

Bricks spawning on collision,Brick game- Instantiating/spawning new bricks on collision

$
0
0
Hello, like many others I'm new to C# and unity, and never got very deep into coding in general. I am building a simple game where bricks spawn one at a time and your goal is to lay them as well as possible. It's still in the very early stages, just trying to get the basic functions down. I have the bricks set to detect collisions, and upon detecting collisions the player is no longer able to move them and a new brick is spawned. This works well for 3-4 bricks, then they start cloning every frame. I've been struggling to find a solution to this for hours, but I don't even know why its happening in the first place. Any help would be greatly appreciated. Thanks! Script is following. using System.Collections; using System.Collections.Generic; using UnityEngine; public class Lay : MonoBehaviour { public Rigidbody2D myRigidbody; public float movementStrength; private bool collided; public GameObject Brick; // Start is called before the first frame update private void Start() { } // Update is called once per frame void Update() { if (collided == false) { if (Input.GetKeyDown(KeyCode.D) == true) { myRigidbody.velocity = Vector2.right * movementStrength; } if (Input.GetKeyDown(KeyCode.A) == true) { myRigidbody.velocity = Vector2.left * movementStrength; } } } void OnCollisionEnter2D(Collision2D collision) { collided = true; Debug.Log("Brick Laid."); Invoke("spawnBrick", 1); } void spawnBrick() { Debug.Log("Brick Spawned."); GameObject newObject = Instantiate(Brick, new Vector2(0, 10), transform.rotation); } }

Viewing all articles
Browse latest Browse all 508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>