- Home /
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;
}
why are you "finding something you already have? and why are you not either doing: try{}catch{}, or if(targ!=null)?
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
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
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.
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?
Answer by flaviusxvii · Sep 13, 2013 at 07:42 PM
var targ = GameObject.FindWithTag("Player" || "ShipLoaded");
You can't use logical or this way.
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;
}
@ChrisSch you should "never" do a FindXXX() especially if you have the object right there
just do targ = trig.gameObject;
Your answer
Follow this Question
Related Questions
Trigger won't recognise tag 1 Answer
OnTriggerStay, add the mass of all objects inside the trigger together 1 Answer
OnTriggerStay still called after moving a parent gameobject 0 Answers
Counting players by trigger Problem 0 Answers
Increasing an GamObjects transform.localScale by 1 with OnTriggerStay 3 Answers