Randomized array result from other scipt

Hail to thee o/

I’ve been fighting with this thing for days, checked various tutorials, searched from older posts and still no vail.

I’m trying to create game level where three controller keys (left,up,right) are randomized every 30 seconds.

Randomized keys what i’m trying to use are alphanumeric and looks like I can’t even do that because INT doesn’t accept letters. Tried even with only numbers but no luck…

My “lottery” script is fully functioning and even debug shows which correct key is chosen.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SatunnainenOhjaus : MonoBehaviour
{
    // Name is RandomController in finnish 

        
    public string[] RandomPick = new string[36] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
   "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
 
    // For testing purposes...
    //  public string[] RandomPick = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    
    bool Waiting = false;

    public int Rerolling; // rolls array number;
    
public int Desperate = 1; // for further debugging the problem

    IEnumerator Reroll()
    {
        Waiting = true;
                
        yield return new WaitForSeconds(3); // counter will be longer when this will work
        
        // Getting random value from string
        System.Random random = new System.Random();
        Rerolling = random.Next(RandomPick.Length); 
        // Showing on console the result which should be available for other script but no vail
        Debug.Log(RandomPick[Rerolling]);
      
        Waiting = false;

    }
    
       void Update()
       {
            if (Waiting == false)
            {
                StartCoroutine(Reroll());

            }
           
       }

}

And incomplete script wich change movement button what is also displayed on UI is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; 
public class Vasen : MonoBehaviour
{
    // Name is Left in finnish
    // UI is for future use after this major problem is solved

    public GameObject Left;
    public SatunnainenOhjaus RandPick;
       
    void Awake()
    {
        RandPick = GetComponent<SatunnainenOhjaus>();
    }
    
          
        

        void Start()
    {
        Debug.Log("Chosen letter/number is: " + RandPick.Rerolling);  // RandPick.Desperate gives same error
    }
        
    
}

But! I’m getting NullReferenceException error and reference is removed from inspector. So I assume It just need more time for lottery script (SatunnainenOhjaus) to get that random number/character. Used Inumerator for Vasen script with a slightly longer time. That didn’t work out.

Next I tried to get normal public int value 1. call it Desperate and Visual Studios Intellisense showed connection, no errors on code even still that didn’t work. :frowning:
I know that INT can only numbers but no luck what so ever with string holding numbers only.

Edit: I’ve added scripts on empty gameobjects and referred them in inspector to see if originally children objects behaved someway differently. Same thing’s going on.

So what should I do to get this “legendary 15 minutes only” project to work? :confused:

Your null reference exception is because RandPick = GetComponent<SatunnainenOhjaus>() is returning null. In order for this to work the SatunnainenOhjaus script and the Vasen script must be on the same game object. Even if you give it the reference in the inspector, whatever reference you give will be overwritten on awake. If you have 2 separate game objects, just remove the GetComponent call and give it the reference in the inspector.

Ah. That pushed it for right way. :slight_smile:

I’ve mixed up that you need to use GetComponent for that certain script everytime you fetch something from that. :stuck_out_tongue:

Now Vasen only gets array index number instead of its name like: SatunnainenOhjaus debugs correct char/number. I assume that I should drop out Vasen script and focus
this all only in SatunnainenOhjaus. Thank you for helping me figure it out. :slight_smile:

Now i’m off to next problem with getting that result to Input.GetKey(“variable”) I think i can handle this one…