I have a cube prefab which i use to instantiate more and more such cubes then later i want to destroy the original cube. Here's the script:
public void drawBlock()
tempRand = (int)Random.Range(0, 2);
Debug.Log("Random Value=" + tempRand);
if (tempRand == 1)
nextBlockPosition = Vector3.forward;
nextBlockPosition = Vector3.right;
GameObject blk = (GameObject)Instantiate(nextBlock, rb.position + nextBlockPosition, Quaternion.identity);
rb = blk.GetComponent<Rigidbody>();
rb.useGravity = false;
rb.AddRelativeForce(-roadSpeed, 0, -roadSpeed, ForceMode.Impulse);
All the cubes have tag of Road. This is what the Collider does(the one which is reponsible for destroying the cubes)
void OnTriggerEnter(Collider col)
Debug.Log("Destroyer Collided with:" + col.name);
Debug.Log(col.name + "was destroyed!");
The problem is that after one of the block gets destroyed, unity starts to give the error that im trying to ref the destroyed object wchich shudn't be the case because i just clone the original cube some of them are still in the scene when the first one gets destroyed. What am I doing wrong here?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
169 People are following this question.
why does unity crash when creating 3d game object
Creating a working phone in Unity
How to click in screen and change color in my sphere?
Canon Ball shooting with Instiate
How to getcomponent on an object instantiated through PUN