Jack Miller
Jack Miller

Reputation: 119

How to access float output variables from other classes

I have a function which will randomly select a number from 1 through to 5, and then inside that function I have an if statement that will assign the numbers output from the function to a variable called randID (for sake of randomly selecting a zone in my game).

In another class, I want to check to see if zoneID is equal to randID (the one I randomly selected earlier) and if so, I want to print a message.

I would do this in one class, however I have multiple objects in my game using this class and I need the main game operator class to decide which of the zones are selected.

Imagine a game of headquarters; that's essentially what I'm trying to figure out. Most of the system is now in place. :)

I thank you in advance for any assistance or guidance you can offer.

PS: I'm using C# and Unity3D to create this project.

Edit: Code.

public static void DecideID() {
    if (gameLeft >= 1f && timeZone_Zone.zoneAlive == false) {
        float random = Random.Range(0f, 5f);
        print (random);
        if(random > 1f && random < 2f) {
            id = 1;
            print (id);
        } 
        else if(random > 2f && random < 3f) {
            id = 2;
            print (id);
        }
        else if(random > 3f && random < 4f) {
            id = 3;
            print (id);
        }
        else if(random > 4f && random < 5f) {
            id = 4;
            print (id);
        }
        else if(random >= 5f) {
            id = 5;
            print (id);
    }
}

The print (id) statements are for test purposes only.

Then I want to do something like this:

if (zoneID = randID) {
    GetComponent<MeshRenderer>().enabled = true;
}

Upvotes: 0

Views: 463

Answers (1)

Liam McInroy
Liam McInroy

Reputation: 4366

What you want is to access the component's variables in another script. Make randID public, and then you can access it in another script.

TypeWithRand randtype;

...

void Start()
{
    randtype = objWithRand.GetComponent<TypeWithRand>();
}

...

if (randtype.randID == zoneID)
{
    ...
}

You will have to replace TypeWithRand with the type that contains the methods for the randID.

Another way to do this would be to have an instance of the class containing randID. Then you could check the value like:

RandClass randClass;

...

if (randClass.randID == zoneID)
{
    //do stuff
}

This implies that you call DecideID (the way I have fixed below) in the constructor of RandClass. You could also keep the function as a void, but then set the value of randID in your function similar to how you set id. Then you would call the function from the instance of the class

randClass.DecideID();

if (randClass.randID == zoneID)
{
    //do stuff
}

Also, your DecideID function incorrect in a few ways. First, Random.Range() can use ints, without the need for the multiple if statements.

id = Random.Range(0, 5);

Also you can return the value of id, and reference it later, so randID's assignment would look like

randID = DecideID();

So, your actual function would look like:

public int DecideID() {
    id = 0;
    if (gameLeft >= 1f && timeZone_Zone.zoneAlive == false) {
        id = Random.Range(0, 5);
    }
    return id;
}

or

public void DecideID() {
    if (gameLeft >= 1f && timeZone_Zone.zoneAlive == false) {
        randID = Random.Range(0, 5);
    }
}

Upvotes: 1

Related Questions