- Home /
HELP! Scripts looks fine but doesn't respond?
first of all here is the script
public Text Redtext;
public Text Bluetext;
public Text Bigtext;
bool RedisOn;
bool BlueisOn;
public float delayTimer;
public float spwanMostwait;
public float spwanlesswait;
float timer;
int RedScore;
int BlueScore;
void Start () {
StartCoroutine (Spwaner ());
timer = delayTimer;
RedScore = 0;
BlueScore = 0;
}
// Update is called once per frame
void Update () {
Redtext.text = " " + RedScore;
Bluetext.text = " " + BlueScore;
delayTimer = Random.Range (spwanlesswait,spwanMostwait);
timer -= Time.deltaTime;
if (timer <= 0) {
if (delayTimer < 10) {
RedText ();
timer = delayTimer;
}
if (delayTimer > 11) {
BlueText ();
timer = delayTimer;
}
}
}
public void RedText (){
Bigtext.text = "Red";
RedisOn = true;
BlueisOn = false;
}
public void BlueText(){
Bigtext.text = "Blue";
RedisOn = false;
BlueisOn = true;
}
void OnCollisionEnter2D (Collision2D col) {
if (RedisOn == true && BlueisOn == false) {
if (col.gameObject.tag == "Red") {
RedScore += 1;
}
if (col.gameObject.tag == "Blue") {
Destroy (gameObject);
}
}
if (BlueisOn == true && RedisOn == false) {
if (col.gameObject.tag == "Blue") {
BlueScore += 1;
}
if (col.gameObject.tag == "Red") {
Destroy (gameObject);
}
}
}
IEnumerator Spwaner() {
while (true) {
yield return new WaitForSeconds (delayTimer);
}
}
my problem is the "BigText" doesn't show , which means that the delayTimer doesn't respond.
but when I change the delaytimer to timer in the if (delayTimer < 10) line, it works but I get no Random, I tried everything here but no solution so how I make this work so I get random text shown.
in the start you are assigning timer = delayTimer;
and you are decreasing timer in the update, you are checking whether timer value less then or equal to zero, let suppose you have timer value 5 then condition never gets true because 5 is not less then or equal to zero. i think you should check greater then zero ins$$anonymous$$d of less then zero. Because your both conditions are inside timer less then zero bracesOther thing is that you are taking time as integer in the condition you should check timer <= 0.0f
, never the value of timer be equal to zero.
lots of confusions in your code logic, these conditions never comes true according to your logic
if (delayTimer > 11)
{
BlueText();
timer = delayTimer;
}
Because you assign again timer = delayTimer;
in the above condition. Other thing is you are choosing delaytimer
randomly and checking hard code values in the conditions?
Answer by Bunny83 · Jul 14, 2017 at 10:01 AM
What values have you set for all your variables? Especially "spwanlesswait" and "spwanMostwait"?
You also have a dead-zone in your "delayTimer" range. If the value is between 10 and 11 nothing will happen. Though since you would constantly roll a new value it's probably not a problem.
Next weird thing is, why do you use Random.Range outside of the if(timer <= 0)
check? You only need a new random number when you enter that block.
Finally a logical problem. You use the delayTimer value to reset your timer. However "red" is only choosen when delayTimer is "small" (less than 10). "Blue" is only choosen when delayTimer is "large" (greater than 10 or 11). That means on average the most time the color will be Blue as blue always gets the longer wait times. This seems very weird.
Finally what is the "Spawner" coroutine good for? At the moment it does nothing besides waiting. Since you change "delayTimer" constantly it's quite unclear for how long the coroutine waits. It certainly is not in sync with your "timer".
You probably want something like this:
timer -= Time.deltaTime;
if (timer <= 0)
{
timer = Random.Range (spwanlesswait, spwanMostwait); // set a new random wait time
if (Random.value < 0.5f) // 50:50 chance to pick either red or blue
{
RedText ();
}
else
{
BlueText ();
}
}
ps: you have a typo in your "spwanlesswait" and "spwanMostwait" variables. You also use inconsistent casing. They probably should be "spawnLessWait" and "spawnMostWait".
I solved it in a different way, I add an invokerepeating to repeat a Random method and the Red and Blue text will set active at random time set by the Random function. , . void Start () {
InvokeRepeating ("random", 5, 5);
RedScore = 0;
BlueScore = 0;
}
public void random () {
rand = Random.Range (0, 10);
}
// Update is called once per frame
void Update () {
Redtext.text = " " + RedScore;
Bluetext.text = " " + BlueScore;
if (rand < 5) {
RedText ();
}
if (rand > 5) {
BlueText ();
}
}