- Home /
Make a trigger change a static variable
Hello,
I wanted to make a script that would change a static variable that's attached to several objects that change their render toggle. The game has an isometric view, and rooms will become visible when tripping over one of a few room triggers that will change the camera's position, and render the room the player is currently in. The triggers would have their own value and would then change the one static variable. So I can change the trigger variable for each trigger object.
The problem I'm facing is that when hitting the trigger, the variable automatically sets itself to 0. If I take out the trigger variable and just place in [cameraIsometric.roomNumber = 1], the number changes appropriately. So the variable does not change. What would be the best option to change the variable?
Heres what I got down:
(triggerExperiment) [Trigger code]
var setMyNumber:int; var target: Transform;
function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; }
function Update() {
}
function OnTriggerStay() {
if(target.gameObject.tag == "Player"){
collider.isTrigger = true;
print("Collision On");
cameraIsometric.roomNumber = setMyNumber;
print(setMyNumber);
//print(cameraIsometric.roomNumber);
}
} function OnTriggerExit() { if(target.gameObject.tag == "Player"){ collider.isTrigger = true; print("Collision Off"); } } (cameraIsometric) [Changes render/camera position]
static var roomNumber : int = 0;
function Awake () { //roomNumber = 0; } function Update() { if(roomNumber == 0) { transform.position = Vector3(-9,13,-43); } if(roomNumber == 1) { transform.position = Vector3(9,12,24); } if(roomNumber == 2) { transform.position = Vector3(-87,28,3); }
}
Answer by aldonaletto · Aug 17, 2011 at 08:12 PM
You are comparing the wrong thing: target.gameObject.tag will always be Player, since you've set the player's tag at the Inspector. You must get the collider that entered the trigger (it's passed as the OnTriggerEnter/Stay/Exit function's argument). You should use OnTriggerEnter instead of OnTriggerStay, since it will be executed only when the player entered the room (OnTriggerStay will be called while the player is inside the room). Finally, remember to set setMyNumber to the room number in each trigger in the Inspector.
var setMyNumber:int; // set the room number in each trigger at the Inspector var target: Transform;
function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; }
function OnTriggerEnter(obj:Collider) { // use OnTriggerEnter
if(obj.tag == "Player"){ // compare the tag of the object that entered the trigger
// collider.isTrigger = true; <- isTrigger must be set at the Inspector print("Collision On"); cameraIsometric.roomNumber = setMyNumber; print(setMyNumber); //print(cameraIsometric.roomNumber); } } function OnTriggerExit(obj:Collider) { if(obj.tag == "Player"){ // the "obj" variable access tag directly // collider.isTrigger = true; <- you don't need this print("Collision Off"); } }
$$anonymous$$uch obliged for your help. I also noticed I placed my trigger experiment code on the player object itself, which was not necessary and likely to cause it to automatically set the variable to 0. With that and your suggestions, the room triggers work correctly.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183021im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script effects all gameobjects. 1 Answer
Global Variables Refuse to Cooperate 1 Answer
Increasing a score value? 2 Answers
Will static variables not work if negative? 1 Answer
Spawning objects on trigger does not work as expected 2 Answers