简体   繁体   中英

Unable to get variables from script in other game object

I'm trying to generate 6 random ints, then assign them to 6 different objects, however I've found that simply generating the int within the object results in every number being the same, due to them all loading at identical times. To work around this issue I'm trying to generate the 6 in a row, so that the clock timing is different and gives a different number. I'm trying to figure out how to access these 6 variables.

I've tried a variety of different formats using 'GetComponent<>()' and 'GameObject.Find("")' together and storing the resulting script in a monobehaviour varibale (which I think is what might be messing me up), then trying to access the numbers through the variable I'm storing the script in. But when I do this, unity says there is no number variable (or Num1, Num2, etc. in my case) in the script.

Here is what I have, I'll only show the code trying to acquire the variables as I have verified that the variable generation works.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class NumberButton : MonoBehaviour
{
    public int Num;
    public GameObject Game;

    void Start()
    {
        MonoBehaviour Game = GameObject.Find("Game").GetComponent<GameInit>();
        print(Game.name);
        if (gameObject.name == "Button1")
        {
            Num = Game.Num1;
        }
        else if (gameObject.name == "Button2")
        {
            Num = Game.Num2;
        }
        //There are 4 more of these, 1 for each button, I realize this is terrible code etiquette.
        GetComponent<TextMesh>().text = Num.ToString();
    }
}

If it works, each button should be assigned their variable based on their name, so Button1 gets Num1 from the GameInit script stored in the GamObject "Game". In actuality nothing is grabbed from GameInit, all the Num variables in the grabber objects are equal to 0, despite the variables in GameInit not being 0.

EDIT

Sorry, I misread exactly what your issue was. The compiler is telling you that those variables do not exist in the MonoBehaviour object, because MonoBehaviour does not have those variables.

You need to change

MonoBehaviour Game = GameObject.Find("Game").GetComponent<GameInit>();

To

GameInit Game = GameObject.Find("Game").GetComponent<GameInit>();

ORIGINAL

I can't yet comment due to low rep so I am going to have this as an answer.

Firstly, is the number generation happening in GameInit.Start()? If so, it is possible that the button Start()s are being called before GameInit.Start() and so all of the variables are the default value of 0 at the time of access. To ensure that this is not the case, put the number generation code in GameInit.Awake().

Also, to ensure that there is no hocus pocus happening with the MonoBehaviour object, try this:

...
if (gameObject.name == "Button1")
{
   Num = GameObject.Find("Game").GetComponent<GameInit>().Num1;
}
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM