- Home /
Why doesn't this work?
What happens is when "change" = true, it will change the gameObjects tag to "Player", but not the var player's tag to "notActive". This script is attached to the player and another not active player. The player's tag is "Player", and the not active one's "notActive". Any help is appreciated.
private var change : boolean = false; private var player : GameObject;
function Update() { player = gameObject.FindWithTag("Player"); Debug.Log(player); }
function LateUpdate() { if(this.tag == "notActive") { if(Vector3.Distance(transform.position, player.transform.position) <= 3) { if(Input.GetKeyDown("w")) { change = true; } } }
if (change)
{ if(this.tag == "notActive") { this.tag = "Player"; player.tag = "notActive"; } else { this.tag = "notActive";
} change = false; } }
Edit: This code works now.
Answer by loopyllama · Mar 17, 2011 at 05:28 AM
if (change)
{
if (this.tag == "notActive")
{
this.tag = "Player";
}
else
{
this.tag = "notActive";
}
change = false;
}
That did not work. It does find the var "player" fine, but it won't change the tag even though it changes the other object's tag to "Player".
I found the problem. It will change the tag to "notActive", but it changes it back to "Player" in the next frame.
that additional info is helpful. try this. the problem is the player B is changed properly then the next frame Player A changes player B back. this change makes sure the object only toggles its own tag.
This worked! Thank you so much. This script was probably the most important script for my game so far. Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why?? MissingComponentException: 2 Answers
Code error Javascript GUI text 0 Answers
Ontriggerenter not working 1 Answer
Freeze game using timer from start, then resume play when click the button. It is possible ? 0 Answers
Underwater Collision Problem 3 Answers