• 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 ChrisSch · Sep 13, 2013 at 07:32 PM · triggernullreferenceexceptiontagmovetowardsontriggerstay

MoveTowards doesn't work after changing tag.

Hi, I have a ship tagged "Player" that gets followed by the boomer objects, that part works perfectly, and when the player loads the cargo, tag changes to "ShipLoaded" as intended, but the boomers no longer follow the player, even tho I specified to choose from tags "Player" || "ShipLoaded", and the console reports NullReferenceException error in OnTriggerStay, on the "var targ" line.

What am I doing wrong?

 #pragma strict
 
 var speed : float = 4;
 var closeEnough : boolean = false;
 
 function Start()
 {
 }
 
 function Update()
 {
 }
 
 function OnTriggerStay(trig : Collider)
 {
     rigidbody.isKinematic = false;
     if(((trig.gameObject.tag == "Player") || (trig.gameObject.tag == "ShipLoaded")) && (closeEnough == false))
     {
         var targ = GameObject.FindWithTag("Player" || "ShipLoaded");
         transform.position = Vector3.MoveTowards(transform.position, targ.transform.position, speed * Time.deltaTime);
     }
 }
 
 function OnTriggerExit(trig : Collider)
 {
     rigidbody.isKinematic = true;
 }
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 gardian06 · Sep 13, 2013 at 07:38 PM 0
Share

why are you "finding something you already have? and why are you not either doing: try{}catch{}, or if(targ!=null)?

avatar image ChrisSch · Sep 13, 2013 at 07:42 PM 0
Share

Because I don't know, or forgot, how to assign the tagged object to var targ? :S And I don't know what try{}catch{} is. :S

avatar image gardian06 · Sep 13, 2013 at 08:03 PM 0
Share

try{}catch{} is commonly referred to as exception handling (think of it as an if statement for

 try{ // if no error do everything that follows
 }catch{ // else abandon everything in try, and do this ins$$anonymous$$d
 }

the reason it is called exception handling is catch can be given specific types of exceptions as arguments for additional information see: http://msdn.microsoft.com/en-us/library/system.exception.aspx#TryCatch

2 Replies

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

Answer by gardian06 · Sep 13, 2013 at 08:14 PM

what your looking for in you onTriggerStay() is

 var targ : Transform;
 var _trans : Transform;
 
 function Start(){
     _trans = transform;
 }
 //...
 function OnTriggerStay(trig:collider){
     if((targ == null) && ((trig.tag == "Player") || (trig.tag == "ShipLoaded")) && (closeEnough == false)){
          var targ = trig.transform;
     }
     if(targ != null){
          trans .position = Vector3.MoveTowards(_trans.position, targ.position, speed * Time.deltaTime);
     }
 }
 function OnTriggerExit(trig:collider){
     if(((trig.tag == "Player") || (trig.tag == "ShipLoaded")) && targ != null){
         targ = null;
     }
 }

this should not only do what you want, but also be better in terms of performance as well.

Comment
Add comment · Show 1 · 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 ChrisSch · Sep 13, 2013 at 08:40 PM 0
Share

Thanks since your answer is more in depth I'll accept your answer. xD But really thank you both! Both answers were really helpful.

I don't fully understand yours gardian06 but I'll study it.

What I don't understand is the targ == null part. If the tag change would happen while the ship was in the trigger, wouldn't that prevent the object to follow the player till it exits the trigger and targ goes null again?

avatar image
1

Answer by flaviusxvii · Sep 13, 2013 at 07:42 PM

var targ = GameObject.FindWithTag("Player" || "ShipLoaded");

You can't use logical or this way.

Comment
Add comment · Show 2 · 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 ChrisSch · Sep 13, 2013 at 07:51 PM 0
Share

Thank you! I did not know that. xD

Here's the new working script if anyone else runs into this problem.

 #pragma strict
 
 var speed : float = 4;
 var closeEnough : boolean = false;
 
 function Start()
 {
 }
 
 function Update()
 {
 }
 
 function OnTriggerStay(trig : Collider)
 {
     rigidbody.is$$anonymous$$inematic = false;
     if(trig.gameObject.tag == "Player" && closeEnough == false)
     {
         var targ = GameObject.FindWithTag("Player");
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targ.transform.position, speed * Time.deltaTime);
     }
     else if(trig.gameObject.tag == "ShipLoaded" && closeEnough == false)
     {
         var targ2 = GameObject.FindWithTag("ShipLoaded");
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targ2.transform.position, speed * Time.deltaTime);
     }
 }
 
 function OnTriggerExit(trig : Collider)
 {
     rigidbody.is$$anonymous$$inematic = true;
 }
avatar image gardian06 · Sep 13, 2013 at 08:16 PM 1
Share

@ChrisSch you should "never" do a FindXXX() especially if you have the object right there

just do targ = trig.gameObject;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger won't recognise tag 1 Answer

Check Trigger Collision's Name/Tag 1 Answer

Trigger not detecting tag 1 Answer

OnTriggerStay simple question 1 Answer

Combining Triggers 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