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 Styxer · Jan 13, 2014 at 02:50 PM · guibooleanrandom.range

PowerUp problem(s)

hey guys i have 2 problems with my powerupscripts: 1.im getting this warning: Assets/Scripts/PowerUpScript.cs(6,15): warning CS0649: Field PowerUpScript.random' is never assigned to, and will always have its default value 0'" and i don't know why its happening.

2.this is a bit more complex:i want to a GUI to deploy after few second to disappear but with my code it happening all the way around which means after the power was Instantiated the GUI pops and only after it hits my paddle it disappears

 using UnityEngine;
 using System.Collections;
 
 public class PowerUpScript : MonoBehaviour {
 
     float random;
     float timer = 3;
     bool myVisibleBool = false;
 
     // Use this for initialization
     void Start () {
         rigidbody.AddTorque( Vector3.forward * 60f );
 
     }
     void Update()
     {
         if(Input.GetKeyDown (KeyCode.Space))
             myVisibleBool = !myVisibleBool;
     }
 
     void OnGUI(){
         if (timer >= 1 && random == 1 && myVisibleBool == false){
             GUI.Label(new Rect(280, 500, 100, 20), "Another Ball Spawned");
         }
         
         if(timer >= 1 && random == 0 && myVisibleBool == false) { // timer is <= 0
             GUI.Label(new Rect(280, 500, 100, 20), "+50 Points");
         }
     }
     
     
     void OnTriggerEnter (Collider other) 
     {
 //        random = 1;
         Random.Range (0, 1);
         PadleScript paddleScript = GameObject.Find ("paddle").GetComponent<PadleScript>();
         if(random==0)
             paddleScript.AddPoints (50);
         if (random == 1)        
             paddleScript.spawnball ();
 
 
         Destroy (gameObject);
     }
 }

p.s thanks to RyanPaterson which helped me

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 KellyThomas · Jan 13, 2014 at 03:08 PM

One:

The first issue is because you never change the value of random, as such it will hold the default value of 0. The solution is to either:

  • remove it

  • define it as constant

  • or use it to hold changing values

Two:

Your second issue is somewhat related, in OnGUI() you have the following condition:

 if (timer >= 1 && random == 1 && myVisibleBool == false){
     GUI.Label(new Rect(280, 500, 100, 20), "Another Ball Spawned");
 }

now this tests the values of three variables:

  • timer, which is initialised to 3 but not changed afterwards

  • random, which will hold 0 as discussed above

  • myVisibleBool, which will toggle when the player presses space.

As such the GUI is controlled solely by the player pressing space.

Comment
Add comment · Show 5 · 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 Styxer · Jan 13, 2014 at 11:53 PM 0
Share

For the 1st issue i think i accidentally deleted random = random.range(0,1); //in this case it should work?

For the 2nd problem i thought my "IF"'s so each one would have; timer -= Time.deltaTime; and my biggest difficulty is about my bool variable which i cant figure out what to do with it (the space bar is only temporary solution)

avatar image KellyThomas · Jan 14, 2014 at 12:47 AM 0
Share

random = random.range(0,1) will remove the compiler warnings but that may be putting the cart before the horse.

Think about the behavior you want and then write code such it realizes that behavior. Program$$anonymous$$g is always easier with some kind of plan.

avatar image Styxer · Jan 15, 2014 at 05:55 PM 0
Share

about the random thingy:it seems i dont get,from the documentation: "static float Range(float $$anonymous$$, float max); Description

Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive]" whats wrong with what im doin?

and about my 2nd problem -what you sail helped alot i now writing a way around it with any bool variables

avatar image robertbu · Jan 15, 2014 at 05:59 PM 1
Share

Since you are passing integers, read the integer version of Random.Range:

Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive]

That is the integer version is 'exclusive' of the final value. So your Random.Range() on line 35 should be Random.Range(0,2). Currently it will only return 0.

avatar image Styxer · Jan 15, 2014 at 06:12 PM 0
Share

im passing float(random is float as u can see)

and also about my GUI it seems it only work with the first one and then it wont seems to popup

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

Pass bool value from multiple objects to one file to check their values? 1 Answer

Log a quickly changing value without boolean flags 0 Answers

Appearence of the GUI element after a certain action 1 Answer

Can't prevent same color random generation. 0 Answers

Need help with changing bool depending on what array is shooting at 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