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 Nercoe · Sep 30, 2012 at 01:39 PM · variablestaticlink

Calling a static variable (not working)

Hey, I'm having a little problem but I believe I have some of it figured out, I just need the expertise of you guys :) Ok, here's the problem. I am trying to make a new scene load when a variable in a different script hits a specific value. When the variable hits 0 I want the end game screen to load. The problem is this:

I set the if statement to load the level if the variable = 3 (it is 3 on start up) the end game screen loads fine. But if I change the value to lets say... 0, when the variable hits 0 (when I have destroyed all of my chickens) nothing happens. It is like it's not updating, can anyone point me in the right direction? Thank you :)

The Code:

Collison script which I am trying to reference (name: CollisionChicken.js)

 var collisionchicken : GameObject;
 static var chickenHit : int = 3;
 var explosion : Transform;
 var Bwuk : AudioClip;
 var myStyle : GUIStyle;
 
 function OnTriggerEnter(c:Collider)
     {
         if(c.gameObject.tag =="bulletShot") 
     
     {
         audio.PlayOneShot(Bwuk);
             chickenHit--;
                 Destroy (collisionchicken);
                 Destroy (collider);
          var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
         }
     }
 
 function OnGUI()
     {
         GUI.Label(Rect(251, 40, 100, 20), ""+ chickenHit.ToString("f0"), myStyle);
     }  
 


The countdown script (when the variable = 0 I want the end game level to load. Name :Countdown.js)

 var myTimer : float;
 var myStyle : GUIStyle;
 
 function Awake(){
     DontDestroyOnLoad(gameObject);
 }
 
 function Start()
 {
 myTimer = 0;
 
 }
 
 function Update () {
  if(myTimer > -1){
   myTimer += Time.deltaTime;
  }
 }
 if (CollisionChicken.chickenHit==3){
     Application.LoadLevel(2);
 }
 
 function OnGUI(){
  GUI.Label( Rect(250, 10, 100, 20), ""+ myTimer.ToString("f0"), myStyle );
 
 }


If you need any more information just ask, I will be here for a while :)

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

Answer by fafase · Sep 30, 2012 at 01:44 PM

Your if statement is outside the update. It is not run.

 function Update () {
   if(myTimer > -1){
     myTimer += Time.deltaTime;
    }
   if (CollisionChicken.chickenHit<=0){
     Application.LoadLevel(2);
   }
 }

Also, I would use <= instead of ==. I do not know how the game looks but if for any reason you would kill two chickens at the same time, then it will drop to -1 and you never load the next level.

Comment
Add comment · Show 2 · 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 Nercoe · Sep 30, 2012 at 01:52 PM 0
Share

Cheers buddy, I'm such a dope :P

avatar image fafase · Sep 30, 2012 at 01:54 PM 0
Share

No worries, this error actually comes up quite often.

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

11 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

Related Questions

Variable type - accessible from other scripts on one Prefab. 0 Answers

Link 2 variables from 2 different scripts to 3rd script 1 Answer

Global Varible Problem 2 Answers

Accessing another var on another script 1 Answer

Unable to modify a variable in another script 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