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 British89 · May 23, 2012 at 10:55 AM · variableupdatespawn

How do i use a value from a var

Hello, I am having trouble getting a object to spawn in once i hit a certain value, This is what i am trying to do...

 var rank = 0;
 
 
 function Update () {
 
     if (rank == 5){
         Debug.Log("We unlocked first item");
     }
 }


I replaced everything with a Debug to see if its working But it dose not seem to work out when i change the value to 5, Any help would be greatly appreciated. Thanks.

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 save · May 23, 2012 at 11:08 AM

Perhaps you could try something like this instead:

 #pragma strict
 
 private var rank : int = 0;
 
 function Start () {
     InvokeRepeating("RankUp",1,1); //For testing purposes, use RankUp() when condition for ranking up is met
 }
 
 function RankUp () {
     rank++;
     switch (rank) {
         case 1: Debug.Log("Rank is now "+rank); /*Give special values for rank 1*/ break;
         case 2: Debug.Log("Rank is now "+rank); /*Give special values for rank 2*/ break;
         case 3: Debug.Log("Rank is now "+rank); /*Give special values for rank 3*/ break;
         case 4: Debug.Log("Rank is now "+rank); /*Give special values for rank 4*/ break;
         case 5: Debug.Log("Rank is now "+rank); /*Give special values for rank 5*/ break;
         default: Debug.Log("Rank is now "+rank+" which is over the rank limit"); break;
     }
 }

The problem you had is most likely because Unity has serialized the value for rank and put it in the Inspector, changing the value inside the script wont make any difference. Remember to type all your variables (`var rank : int = 0`).

Comment
Add comment · Show 3 · 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 British89 · May 23, 2012 at 11:21 AM 0
Share

Thanks allot works perfect!

1 other thing, Whats #pragma strict, Everything seems to work without it,

Thanks again.

avatar image save · May 23, 2012 at 11:24 AM 0
Share

Good thing! #pragma strict is disabling dynamic typing, which is crucial for optimizing performance. If you just type your variables (and returning functions) then you'll most likely be good to go.

avatar image British89 · May 23, 2012 at 01:28 PM 0
Share

Sorry i just realized something, This all works perfect apart from it checks every 1 frame "InvokeRepeating("RankUp",1,1);"

I have it so a gameObject gets activated for example

 private var rank : int = 0;
 
 function Start () {
     InvokeRepeating("RankUp",1,1);
 }
 
 
 function RankUp () {
     //rank++;
     switch (rank) {
         
        case 10:
        Helicopter.SetActiveRecursively(true);
        break;
        
        default: Debug.Log("Rank is now "+rank+" which is over the rank limit"); 
        break;
     }
 }

So every 1 frame at Rank 10 it is trying to activate the helicopter causing lag spike at every 1 frame, Sorry if there is a simple solution i just cant think of any, Coffee dose not seem to do it for me any more.

Thanks.

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

How often is the update function called ? 1 Answer

Variable Doesn't Update in "Update" function 0 Answers

How to force a variable to prefab 1 Answer

Variable value not updating in test script 1 Answer

variables based on array.length not updating in JS 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