- Home /
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
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 afterwardsrandom, 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.
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)
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.
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
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.
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
Follow this Question
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