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 OnyxRook · Jul 26, 2013 at 09:25 AM · variablesreferencing

Referencing AND affecting a variable from another script

First post, and very new to scripting, so apologies in advance! I've been having a really rough time with this..

I've been reading through unityAnswers, and I understand that you can certainly reference a variable from another script, but what I don't understand is whether or not you can affect it through the secondary script as well.

I think what I'm trying to get at is easier to understand if I explain my scenario. Here's what I'm trying to do:

Player goes through the level and is able to collect torches scattered around on the ground. As they pick them up, it's added to their torch count which appears as a GUI. If the player approaches a section of wall where a torch can be placed, they are prompted to do so, and with a button press a torch appears. At the same time, their torch count is lowered by 1.

I'm trying to achieve this through two scripts. The first attaches to a trigger placed in front of the wall area where a torch can be added. This is it here:

 var placementHint = "If you have a torch, you can place it here with 'b'";
 var needTorchHint = "Looks like you're out of torches...";
 var inTorchTrigger = false;
 var torchPlaceable = false;
 var showHint = false;
 var noTorches = false;
 var MyWidth = 400;
 var MyHeight = 50;
 var gameempty : Transform;
 var torchNumber = torchinventory.GetComponent.count; 
  
 
 function OnTriggerEnter(){
     inTorchTrigger = true;
     showHint = true;
 }
 
 function OnTriggerExit(){
     inTorchTrigger = false;
     showHint = false;
     torchPlaceable = false;
     noTorches= false;
 }
  
  
  
 function Update(){
     if(inTorchTrigger){
     
            if(Input.GetKeyUp(KeyCode.B)&& torchNumber >= 1){
                 showHint = false;
                 torchPlaceable = true;
                 torchNumber -=1;
                 
                 
             }
             else if (Input.GetKeyUp(KeyCode.B)&& torchNumber < 1) {
             showHint = false;
                noTorches = true;
             }
         }              
     
 }
  
  
 function OnGUI(){
     if(inTorchTrigger){
         if(showHint){
             GUI.Box (Rect ((Screen.width/2)-(MyWidth/2), (Screen.height/2)-(MyHeight/2), MyWidth, MyHeight), placementHint);
         }
         if(noTorches){
                GUI.Box (Rect ((Screen.width/2)-(MyWidth/2), (Screen.height/2)-(MyHeight/2), MyWidth, MyHeight), needTorchHint);
  }
  
     if(torchPlaceable){
         gameempty.gameObject.SetActive(true);
            Destroy(gameObject);
                 }
         }
     
 }
  

(I know the torchNumber var is wrong. At some point I had tried some variation where it didn't throw any errors [but still didn't work], but I've tried so many versions of this at this point that I can't find my way back to it.)

And this second one serves as the collected torch count, which is attached to the player:

 static var count = 0; 
 
 
 function OnTriggerEnter (other : Collider ) {
 
 if (other.tag == "unusedTorch") {  
 count ++;
 Destroy(other.gameObject);
 }
 }
 
  
 
 function OnGUI () {
 
     GUI.Label (Rect (400, 20, 130, 60), "Torches:"+count ); 
 
 }
 

I want that count variable to be referenced in the first script AND to be affected by it as well. i.e. The torchCount script states that the player has picked up three torches. Once the player places a torch on the wall, I want that number to be reduced from three to two. Is this possible?

Separately each script functions precisely how they should. Originally I had the var torchNumber simply set to an integer and everything worked like a dream. It wasn't until I went about trying to apply it to multiple wall sections that I realized I needed a separate torchCount, otherwise each time the player approached a trigger, the would have X number of torches again.

I'm guessing that there's a very simple elegant solution that I'm just not seeing here. Again, please forgive this humble noob, I'm trying to get better! :)

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 OnyxRook · Jul 27, 2013 at 12:50 AM

Holy crap I think I solved it...!

Originally, I was trying to pull the variable from the collection script, and declare that as a variable for use in the torch lighting script.

i.e.: var torchNumber = torchinventory.GetComponent.count;

In hindsight that seems kind of dumb. Trying to make a variable a variable... Anyway! I commented that out and decided to approach this from another angle. I just dropped the following in the function to place a torch:

 torchinventory.count -= 1;

IT WORKED! :O My torch inventory number dropped. I decided then to ditch the torchNumber variable, and reference the torchinventory.count directly in my if statement. The result was PERFECT! It did everything I want! I just tested it to make sure that it worked with multiple torch set ups, and it does! Here's my adjusted code:

 var placementHint = "If you have a torch, you can place it here with 'b'";
 var needTorchHint = "Looks like you're out of torches...";
 var inTorchTrigger = false;
 var torchPlaceable = false;
 var showHint = false;
 var noTorches = false;
 var MyWidth = 400;
 var MyHeight = 50;
 var gameempty : Transform;
 
 function OnTriggerEnter(){
     inTorchTrigger = true;
     showHint = true;
 }
 
 function OnTriggerExit(){
     inTorchTrigger = false;
     showHint = false;
     torchPlaceable = false;
     noTorches= false;
 }
  
  
  
 function Update(){
     if(inTorchTrigger){
     
            if(Input.GetKeyUp(KeyCode.B)&& torchinventory.count >= 1){
                 showHint = false;
                 torchPlaceable = true;
                 torchinventory.count -= 1;
                 
                 
             }
             else if (Input.GetKeyUp(KeyCode.B)&& torchinventory.count < 1) {
             showHint = false;
                noTorches = true;
             }
         }              
     
 }
  
  
 function OnGUI(){
     if(inTorchTrigger){
         if(showHint){
             GUI.Box (Rect ((Screen.width/2)-(MyWidth/2), (Screen.height/2)-(MyHeight/2), MyWidth, MyHeight), placementHint);
         }
         if(noTorches){
                GUI.Box (Rect ((Screen.width/2)-(MyWidth/2), (Screen.height/2)-(MyHeight/2), MyWidth, MyHeight), needTorchHint);
  }
  
     if(torchPlaceable){
         gameempty.gameObject.SetActive(true);
            Destroy(gameObject);
                 }
         }
     
 }
  


This was probably blatantly obvious, but I don't care. Solving it has made me feel like a freaking wizard!! :D This whole scripting thing is pretty danged cool!

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 amostajo · Jul 27, 2013 at 02:13 AM 0
Share

OnGUI is kind of acceptable for menus. Try to avoid it for in-game purposes, its performance is crap.

As a recommendation, try other solutions like NGUI.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference specific variable from a specific gameObject? 2 Answers

How can I reference a float variable to a GUI label in another script? 1 Answer

Properly referencing a variable from another script 1 Answer

How to share a variable through multiple copys of the same script 2 Answers

Instance(?) problems 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