How do I create a variable?

In my script I access a public float from another script by using

private Counter playerScript;

playerScript = GameObject.Find("Text").GetComponent<Counter>();
//print(playerScript.myTimer);

I can print the

playerScript.myTimer

But I can’t do something like

if playerScript.myTimer >= 0{
    randomSpawnRate = Random.Range(0.9f, 0.99f);
}

So maybe a variable would solve this?

Thanks!

You have to put it in parenthesis/brackets:

if (playerScript.myTimer >= 0)
{
    randomSpawnRate = Random.Range(0.9f, 0.99f);
}
1 Like

For the record myTimer probably is a variable.

Yea “myTimer” is a variable.

public float myTimer = 0;

But I want to do the same for “playerScript.myTimer” so I don’t always have to put that in if statments.

So find like

float ppp = (playerScript.myTimer);

Although I’m sure that’s not the way to do it.

What is it you don’t want to put in if statements? If playerScript.myTimer is a float, you can use that as @Stardog showed in the reply above. As long as playerScript is actually set to something, it should always work.

If you’re looking to shorten playerScript.myTimer, then you can assign it to a float located in the script you’re using it in.

float pTime = playerScript.myTimer;

if(pTime >= 0)
{
//Do whatever you want here
}

Is that what you mean?

Just be aware that a float is a value type. Doing it this way means pTimer will not update when myTimer changes. Not a problem if you do it immediately before the if. But it won’t work in say Start when the if is in Update.