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 Bartman3010 · Aug 17, 2011 at 07:46 PM · triggervariablestatic

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); }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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"); } }

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 Bartman3010 · Aug 18, 2011 at 12:05 AM 0
Share

$$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

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

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


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