Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MMCDeveloper · Aug 09, 2014 at 08:20 PM · prefabmissingreferenceexceptionstrafe

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Aug 09, 2014 at 08:22 PM 1
Share

How does your script associate the variable called strafeleft with the game object with the same name?

avatar image MMCDeveloper · Aug 12, 2014 at 11:55 AM 0
Share

I manually linked the gameobject to the prefab in the expect. The game object is a child of the main ship prefab itself.

avatar image MMCDeveloper · Aug 12, 2014 at 11:56 AM 0
Share

That's what I get for commenting from my phone. It was supposed to say linked in the inspector.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
    }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges