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 brandonhotdo · Jul 26, 2013 at 07:57 PM · randomvalueifvar

Random value for if statement

i have some code and i'm not sure on how to get a varaible into a if statement.

 #pragma strict
 public var number;
 function Start () {
 var number = Random.Range(2,2);
 }
 
 var myTimer : float = 10.0;
  
 function Update () {
 if (var number == 2){
 transform.position.y = 9.320176;
 transform.position.z = 0;
 transform.position.x = 0;
 }
 }
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 Peter G · Jul 26, 2013 at 08:08 PM

First, welcome to Unity Answers :)

Second, you have two little mistakes that are probably preventing this from compiling.

  1. If you're using static typing (which you should) You should always declare a type with your variables (it can be inferred, but somehow the compiler needs to know what this object is). To do that you should change:

      var number;
    
      to:
    
          var number : int;
    
    
  2. You don't need a var keyword in the if statement. You only want to put var in when you're declaring a variable for the first time. Since you already declared it above, you don't need to do it again. P.S. You did this twice, make sure you fix it in both places that you did it.

       if (number == 2) {
    
    

So your complete code should look like.

 #pragma strict
 public var number : int;
 function Start () {
      number = Random.Range(2,2);
 }
  
 var myTimer : float = 10.0;
  
 function Update () {
     if (number == 2){
         transform.position.y = 9.320176;
         transform.position.z = 0;
         transform.position.x = 0;
     }
 }
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 brandonhotdo · Jul 26, 2013 at 08:25 PM 0
Share

Thank you!

avatar image
0

Answer by gregzo · Jul 26, 2013 at 08:10 PM

Hi Brandon, Couple of things : 1) The keyword var is used to declare a variable. Where you declare it matters very, very much : declare it in a function, and the variable will only be valid in that function ( it's called a local variable ). What you are doing there is declaring number in Start, and then in update declaring a new variable also called number, which will by default be 0.

2) Variables should be typed. Not in JS, where the compiler will try to infer type, but if you're learning, type your variables. var number : int; ( or float or double or whatever the type you want your number to be ).

3) Random.Range (2,2) is not much of a range... Give me a number between 2 and 2 ? Worse, give me a number between 2 and 2, excluding 2 ? ( http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html )

Either you want to pick a random value once ( in Start ), and do something accordingly, or pick a random value every frame, and do something accordingly. Do clarify what you're trying to achieve! Certainly not locking transform.position every frame to the same value if number == 2.

4) transform.position is a Vector3. UnityScript ( JS ) allows you to do transform.position.x = whatever by behind the scenes replacing the line by transform.position = new Vector3 ( whatever, transform.position.y, transform.position.z ); So you will ask 3 times less from the cpu if you directly assign a Vector3 to transform.position instead of setting x y z seperately.

Hope it helps!

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 tw1st3d · Jul 26, 2013 at 08:14 PM 0
Share

All you did was explain why he did it wrong, ins$$anonymous$$d of help him fix it. Consider trying to fix someones code in the future ins$$anonymous$$d of going on about how you create a variable.

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

19 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 avatar image avatar image avatar image

Related Questions

BCE0023: No appropriate version of 'UnityEngine.Random.Range' for the argument list '(System.Object)' was found. 0 Answers

Unity keeps saving Scene 0 Answers

How do Generate Random Unique int ? 1 Answer

Time elapsed between variable change? 1 Answer

Random value from -1 to 1 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