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 dadrester · Jan 16, 2012 at 07:25 AM · booleanstaticglobalvarible

lock and key mechanic: how to use variables across scripts

I'm trying to do something relatively simple but getting stuck. just wondering if I could get a little help. I'm pretty new to scripting in unity and don't come from a coding background so I'll try and show my current workings.

I have 2 game objects. One is a card that the player collects which has this script (CollectedCard.js) attached:

 var card : GameObject;
 static var cardCollected: boolean;
 
 function OnTriggerEnter() {
 cardCollected = true;
 card.active = false;
 }

I also have an empty gameObject with a timer script attached that (amongst other things) counts down and when the timer reaches zero performs this check:

 static var Collected = CollectedCard.cardCollected;
 var success: String ; //level to load on success
 var fail: String ; //level to load on fail

 if (restSeconds == 0) 
         if (Collected) 
         Application.LoadLevel(success);
         else
         Application.LoadLevel(fail);   

What I'm trying to do is; once the player has collected the card (detected in one script) set a bool to true in the other so that it loads a specific "success" scene, or else load a "fail" scene. What's happening is that it always loads the "fail" scene.

Now I'm firstly not too hot on my syntax anyway, but I'm not even sure if I'm doing this correctly so any pointers would be greatly appreciated. I understand it's pretty basic, but I couldn't quite find what I was looking for elsewhere.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by syclamoth · Jan 16, 2012 at 08:33 AM

Your syntax is fine, but your logic is a little off.

It seems that you are assuming that the line

 static var Collected = CollectedCard.cardCollected;

causes 'Collected' to always refer to CollectedCard like a reference type. Of course, booleans are a value type- which means that at the moment that line is declared, Collected will be set to whatever CollectedCard.cardCollected was at that moment, and there is no further connection between the two, and when you use it in your 'if' statement it will still hold the value that CollectedCard.cardCollected had when it was declared, which is of course false.

If you remove the variable entirely, and just use

 if(CollectedCard.cardCollected)

it will remove this problem entirely.

Of course, using static variables is prone to problems, especially if you are planning to have the ability to restart the level. If you reset the level at runtime, the variable 'CollectedCard.cardCollected' won't get reset automatically, since it will remember the value from the previous playthrough. If you instead make it an instance variable, and then keep a reference to that instance in your counter script, it won't have any of the same problems.

 var card : GameObject;
 var cardCollected: boolean;
 
 function OnTriggerEnter() {
     cardCollected = true;
     card.active = false;
 }

 var cardCollector : CollectedCard;

 if(cardCollector.cardCollected)
 {
     // success!
 } else {
     // fail.
 }

In your second script, just drag the gameobject with the CollectedCard component attached to it onto the 'cardCollector' variable in the inspector, and it will work fine.

Comment
Add comment · Show 7 · 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 dadrester · Jan 16, 2012 at 08:44 AM 0
Share

Round-tripping the level was going to be the next problem I faced. Thanks for the explanation. It's always helpful to know "why" rather than just "how".

avatar image dadrester · Jan 16, 2012 at 03:40 PM 0
Share

Got it all working nicely a little while ago with my test object. Now I've replaced that with a swanky looking card which is made up of 2 differently textured planes sitting back to back.

Problem is that I'm only disabling the parent plane (the backface is a child of this). Is there a way in script of disabling both other than telling it each object to disable? I know I could just manually disable both easily enough, but for future reference, where an object might have multiple children, knowing how to do this would be very useful.

avatar image syclamoth · Jan 17, 2012 at 03:09 AM 0
Share

Use GameObject.SetActiveRecursively(boolean). This is like setting the GameObject.active for every object in the hierarchy!

avatar image dadrester · Jan 17, 2012 at 06:26 AM 0
Share

excellent. thanks

avatar image dadrester · Jan 17, 2012 at 07:21 AM 0
Share

Just a final question. I'm sure it must be, but how would you set $$anonymous$$eshRenderer to be deactivated for multiple objects recursively? Probably a better question might be, where can I find all this syntax :) so I don't have to keep asking daft questions.

Show more comments
avatar image
1

Answer by dadrester · Jan 16, 2012 at 08:26 AM

Sorted it! I removed the static var Collected = CollectedCard.cardCollected; and then added the reference to the other script directly into the function, so:

 if (restSeconds == 0) 
     if (CollectedCard.cardCollected)
     Application.LoadLevel(success);
     else
     Application.LoadLevel(fail) ;

Took a bit of jiggling about semi-colons and "squiggly brackets". I guess I really ought to learn the logic behind the syntax :)

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 syclamoth · Jan 16, 2012 at 08:33 AM 0
Share

Wow, got ninja'd by the OP... Anyway, you should stop using static variables, they're dangerous.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Global variables - static keyword ? UnityScript? javascript? 1 Answer

Need help accsesing a scripts variable alfter getting the script cached. 1 Answer

Accessing non static members in a static function argument?? 1 Answer

Static Variable Problem 1 Answer

Global Variables Refuse to Cooperate 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