- Home /
Trying to access destroyed object
Ok, I am new to coding so have patience! Im making my first simple project, where the player (a cube) is moving constantly forward, collides with things (spheres and cylinders) on a straight road and collect points on collision. The sphere and cylinder creates a particle effect on collission and then disappears.
I have created prefabs of the cylinder, placed copies of them onto the road (all works fine). I created a sphere, created a prefab of it, and added the same code as the cylinders have! But now "only" the first sphere prefab works...
The following weird thing happens! Also the following sphere prefabs works AS LONG AS THEY ARE REALLY CLOSE, and gets a collission within aproximately 1 second AFTER collission with first sphere.
If I increase the distance between the spheres on the road (so time between collisions are more than 1 sec), I get an error message:
MissingReferenceException: The object of type ParticleEmitter has been destroyed but you are still trying to access it. Your script should either check if it si null or you should destrouy the object.
related to this another thing happens... if I skip to collide with the first sphere and only collides the last ones (that I have copy - pasted into the scene withing UNITY3D editor), the last ones will give the error immediately (at first collision forward), even if all collisions happened within a second (so here the 1 sec rule doesnt seem to happen).
Every cylinder and sphere have: Collider - Is Trigger activated.
here is the code attached onto spheres and cylinders:
// spelaren = player (cube)
var Partikelexplosion : ParticleEmitter; function OnTriggerEnter (myTrigger : Collider) { if(myTrigger.gameObject.name == "spelaren") { Instantiate(Partikelexplosion, transform.position, transform.rotation); Destroy(gameObject); } }
This code works WELL on the cylinders, no matter how far away from each other they are. Can anybody help me, and explain clearly for a beginner plz.
Answer by Ashkan_gc · Mar 15, 2011 at 01:24 PM
the particle emmiter object that you attached to your spheres might not be a prefab and is an object in your scene which will be destroyed after showing the effect. it might be the answer because it's not clear to me why the last sphere could not be collided even at the first time. i suggest that you do all you did again. 1 create an sphere and add the script to it. 2 make a prefab from it. 3 add the emmiter object to the prefabs explosion property in your script. 4 place instances of the prefab in your scene. the code is fine. however using the Plaer tag instead of the player object's name is faster. set the tag of the cube to player and try to check it's tag in Trigger events instead of the gameObject's name.
THX many times Ashkan! The particle to the spheres were not PREFABS! I changed that, and made all speheres from 0, as u described, and now they work fine! Im real happy now thanks to u! :-)
actually unity answers is here to make us happy! nothing special boy! it's happy cause you are happy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112938im_/https://answers.unity.com/themes/thub/images/avi.jpg)