- Home /
Missing reference exception
I've been reading though the forums, and haven't found anything that points to my particular issue... this is seriously killing me. I get this message in the console: "MissingReferenceException: The variable strafeleft of 'Asteroidlvl10' doesn't exist anymore. You probably need to reassign the strafeleft variable of the 'Asteroidlvl10' script in the inspector. UnityEngine.Transform.get_localPosition () Asteroidlvl10.Update () (at Assets/Scripts/Asteroidlvl10.js:98)"
here is the snippet of code that it points too:
if(Input.GetButton("a"))
{
mark = 0;
speed = (movespeed/100) * distance;
movedirection = strafeleft.localPosition * distance;
movedirection.y = -12;
movedirection.x = movedirection.x * 10;
movedirection.z = movedirection.z * 10;
transform.position = Vector3.MoveTowards(transform.position, movedirection, speed*Time.deltaTime);
timerdrift = speed;
speed1 = speed;
}
I have created an asteroid prefab, and added a strafeleft empty gameobject to my ship. this is also a prefab. when the game starts asteroids are instantiated infront of the ship off screen. They fly past the screen based on the speed dictated in another line of script. The problem is that when the a key is depressed instead of the asteroid moving towards the strafeleft position it complains that the refernce doesn't exist. in the instantiated clones of the asteroid prefab the gameobject strafeleft is missing. I assure you that the reference was made between the two prefabs and the prefabs are still intact in the project window. for the life of me I can't figure out why the asteroid prefab drops strafeleft, but doesn't drop any of the other references including ship which is a gameobject reference for the parent of strafeleft.
I hope that makes sense, but I really need help with this one. I've been messing with it for a couple of days and can't figure it out.
How does your script associate the variable called strafeleft
with the game object with the same name?
I manually linked the gameobject to the prefab in the expect. The game object is a child of the main ship prefab itself.
That's what I get for commenting from my phone. It was supposed to say linked in the inspector.
Answer by Josh1231 · Aug 11, 2014 at 03:15 PM
you may want to use error checking, for missing reference it is
catch(MissingReferenceException)
then find the game object again
strafeleft = GameObject.Find("StrafeLeft");
continue;
If it doesn't find it then it was probably deleted from the scene somehow, maybe one of the scripts did it by accident. You may need the continue there to continue the script after that part is done, but that's only if there is things after that part you need.
You're also going to have to put a try over input part, it should look something like this:
try
{
if(Input.GetButton("a"))
{
mark = 0;
speed = (movespeed/100) * distance;
movedirection = strafeleft.localPosition * distance;
movedirection.y = -12;
movedirection.x = movedirection.x * 10;
movedirection.z = movedirection.z * 10;
transform.position = Vector3.MoveTowards(transform.position, movedirection, speed*Time.deltaTime);
timerdrift = speed;
speed1 = speed;
}
}
catch (MissingReferenceException)
{
strafeleft = GameObject.Find("StrafeLeft");
continue;
}
Your answer
Follow this Question
Related Questions
When the player dies and I reload the scene the enemies spawn script stops working. 2 Answers
When the original enemy dies, the spawns stop (javascript) 2 Answers
How do you destroy a prefab, but not the original object? 1 Answer
Referenced script behaviour is missing only when overwriting prefabs ? 0 Answers
MissingReferenceException after destroying a prefab? 2 Answers