Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ScotchNail · Jan 07, 2015 at 09:16 AM · variableontriggerenterboolean

Multiple objects sharing a boolean variable in a script.

In my 2D game, I have multiple GameObjects that represent ladders. I have a script named "LadderClimb.js" attached to these objects. In that script, there is a boolean variable that determines if the player is on the ladder. I am accessing this variable in another script used to animate the player (play climbing animation when onLadder is true) but the boolean variable onLadder is only affected by the last GameObject added to the scene with the script attached.

Here is my LadderClimb.js code

 var onLadder : boolean;
 private var player : GameObject;
 
 function Start () {
     player = GameObject.FindGameObjectWithTag("Player");
 }
 
 function Update () {
 
     if (onLadder) {
         if (Input.GetKey('w') && player.transform.position.y) {
             //Climb up
             player.transform.position.y += 0.21;
         }
         if (Input.GetKey('s') && player.transform.position.y) {
             //Climb down
             //Debug.Log("Climbing DOWN");
                 player.transform.position.y -= 0.21;    
         
         } 
     }
 }
 
 function OnTriggerEnter (other : Collider) {
     if (other.tag == "Player") {
         onLadder = true;
     }
 }
 
 function OnTriggerExit (other : Collider) {
     if (other.tag == "Player") {
         onLadder = false;
     }
 }
 //end LadderClimb.js

I am trying to access the boolean onLadder from this script:

 var ladderClimb : LadderClimb;
 var climbing : boolean;
 
 function Update () {
     
     ladderClimb = GameObject.FindGameObjectWithTag("Ladder").GetComponent(LadderClimb);
     climbing = ladderClimb.onLadder;
     
     //climbing is only true for last object added to scene... why?
     if (climbing) {
         //Code to animate player
     }
 }
 //end PlayerAnimate.js


I have also tried doing this with prefabs but I get the same results. Why is the boolean variable onLadder only affected by the last object added to the scene?

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
1
Best Answer

Answer by GameVortex · Jan 07, 2015 at 09:22 AM

GameObject.FindGameOBjectWithTag will only find one GameObject, and unless something changes, it will find the same GameObject everytime, and it will be the first one Unity finds, which just so in you instance happens to be the last ladder you added to the scene.

The GameObject.Find functions are really slow and should not be used in the Update function. They should be used to find an object once and then store the reference to it.

In your case I would recommend you to have the ladder tell the player that the player is on it by calling a function in the player when the player enters the ladder in the OnTriggerEnter. And have the ladder tell the player it exited it as well in the OnTriggerExit. That would be the easiest solution at the moment.

Functionality wise it usually makes more sense to have the player check what it has entered and react accordingly, but both methods work fine.

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 ScotchNail · Jan 07, 2015 at 09:43 AM 0
Share

Thank you for the advice, it worked!

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to Change a variable in another script when ontriggerenter 0 Answers

if several variables is true then do function 2 Answers

how to create boolean Varibles ? 3 Answers

The more pages the closer he comes. 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