简体   繁体   English

无法从其他游戏对象的脚本中获取变量

[英]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. 我试图生成6个随机整数,然后将它们分配给6个不同的对象,但是我发现,简单地在对象内生成整数会导致每个数字都相同,因为它们都在相同的时间加载。 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. 要变通解决此问题,我试图连续生成6,以便时钟时序不同并给出不同的数字。 I'm trying to figure out how to access these 6 variables. 我试图弄清楚如何访问这6个变量。

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. 我已经尝试过使用'GetComponent <>()'和'GameObject.Find(“”)'一起使用多种不同格式,并将生成的脚本存储在单行为变量中(我认为这可能使我困惑),然后尝试通过将脚本存储在其中的变量访问数字。但是,当我这样做时,Unity表示脚本中没有数字变量(在我的情况下为Num1,Num2等)。

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". 如果可行,应该根据按钮的名称为每个按钮分配变量,以便Button1从存储在GamObject“游戏”中的GameInit脚本中获取Num1。 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. 实际上,没有从GameInit抓取任何东西,尽管GameInit中的变量不为0,但抓取器对象中的所有Num变量都等于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. 编译器告诉您,这些变量在MonoBehaviour对象中不存在,因为MonoBehaviour没有这些变量。

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()? 首先,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. 如果是这样,则有可能在GameInit.Start()之前调用了Start()按钮,因此所有变量在访问时均为默认值0。 To ensure that this is not the case, put the number generation code in GameInit.Awake(). 为确保不是这种情况,请将数字生成代码放在GameInit.Awake()中。

Also, to ensure that there is no hocus pocus happening with the MonoBehaviour object, try this: 另外,为确保MonoBehaviour对象没有发生轨迹,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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