Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by cocopuffs · Jun 19, 2011 at 06:59 AM · transformnullupdate problem

update function problem

my problem is that i keep getting this error MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. i understand that one of my scripts has an update function and is still trying to find this object after it got destroyed ....how can i make the script stop running the update function after the object (cube) gets destroyed????or better yet how can i fix this error?? the script is.......... http://pastebin.com/X2UCPmYf the "question1" thing is the script that it opens once its close to the player

Comment
Add comment · Show 1
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 RodrigoSeVeN · Jun 19, 2011 at 07:15 AM 0
Share

If the error is in this script you posted, then i suppose that the target is the object that is missing. Either this, or the error is in the "question1" script. Can you confirm it? Also, do you mean that the unity editor crashes when this error occur?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GuyTidhar · Jun 19, 2011 at 08:36 AM

Most likely, the reason for this is due to the use of GetComponenet within the Update() function. You should call the getComponent only once before starting the update (enable=true starts the script...). This way, you address any errors of this kind before doing updates and you avoid a bad performance, as the GetComponent function is an expensive one. Do something like this: in the scope of the class:

 private var other;

 void Start()
 {
  other = gameObject.GetComponent("question1");
 }

then use other in update without the need to constantly call GetComponent.

Also, if you know in advance the type of the variable other - define it when you define the variable so you can catch errors before running your program (e.g. if "other" is a variable of type "GameObject" define it like this:

 private var other : GameObject;
Comment
Add comment · Show 5 · 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
avatar image cocopuffs · Jun 19, 2011 at 08:46 AM 0
Share

thank you sooo much!!!!

avatar image cocopuffs · Jun 19, 2011 at 08:50 AM 0
Share

but where would i put the

if ( Vector3.Distance(target.position, transform.position ) < 5) { other.enabled = true; } if ( Vector3.Distance(target.position, transform.position ) > 5) { other.enabled = false;

avatar image GuyTidhar · Jun 19, 2011 at 08:55 AM 0
Share

You can keep it in the Update function. Generally speaking, try to put anything that does not change outside of the update function. When data changes and you need to do something to handle the change, you place it in the update.

avatar image cocopuffs · Jun 19, 2011 at 09:10 AM 0
Share

ohhhh ok thank you:) your the best!

avatar image cocopuffs · Jun 19, 2011 at 09:17 AM 0
Share

so it would be like this???? http://pastebin.com/RJ6btqje

avatar image
0

Answer by GuyTidhar · Jun 20, 2011 at 06:18 AM

It probably would be something like this, since I assume you wish the to change the state of the question1 component depending on the changing distance (doesn't the distance change in your game?)

 var target : Transform;
 private var other;

 void Start()
 {
      other = gameObject.GetComponent("question1");
 }
 
 Update()
 {
     if ( Vector3.Distance(target.position, transform.position ) <= 5)
        other.enabled = true;
     else
        other.enabled = false;
 }

Also, notice that I used only one call to Vector3.Distance since you don't need the second one. Once you have the distance, using if...else you can decide what to do with it. Your distance is either bigger then 5, equals 5 or smaller then 5. If you'd want to do something else when it equals 5 you could still calculate the distance once and use the memory to test, like this:

 update()
 {
    var distance : float = Vector3.Distance(target.position, transform.position );

    if ( distance < 5 )
      other.enabled = true;
    else if ( distance > 5 )
      other.enabled = false;
    else // this means distance == 5 and you don't need to specify the condition now
    {
      // do something else if you wish
    }
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Destroyed Objects causing errors (transform/setdestination) 1 Answer

if ( Transform == null) ?? Empty Transform reference 1 Answer

how can I check if an object is null? 4 Answers

MissingReferenceException Help 1 Answer

Is "null" the best way to unparent a child? 1 Answer


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