0

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.

Kukumi
  • 1
  • 2
  • Have you checked this thread? https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number It might help a lot. – WQYeo Sep 10 '19 at 03:44
  • thank you!! I hadn't found this but yes this helps a lot. However I would still love an answer to the full question as I'm going to need to do what I described in a different scenario later on. – Kukumi Sep 10 '19 at 03:55
  • Where is the code for `GameInit`...? And you should rather use `switch - case` – derHugo Sep 10 '19 at 05:21
  • 1
    Maybe none of the cases `gameObject.name == "Button1"` etc are true so `Num` is never assigned to but keeps it's default value `0`? – derHugo Sep 10 '19 at 05:54
  • So you have button with different names and based on the name you assign the Num value. Why not assign directly the Num value in inspector? It is not a "variable" more like a constant based on which object. You could also use the last digit of the name to populate all values. gameObject.name[gameObject.Name.Length - 1] – Everts Sep 10 '19 at 06:05
  • If you search for something called "Game" its name cant be Button1 or Button2.. did you mean to find by tag or something else? – BugFinder Sep 10 '19 at 07:12
  • @BugFinder made the same mistake at first: The object OP `Find`s is called `Game` **but** `gameObject.name` refers to the object this script is attached to ;) not the `Game` reference – derHugo Sep 10 '19 at 09:19

1 Answers1

2

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;
}
...

Brad_95
  • 21
  • 4