Getting script variables

I’m trying to get an array from another script. The array is public, but for some reason I try to use it I get a nullrefrencerror. Any idea?

int arrayLength;

void Start () {
		
		GameObject thePlatform = GameObject.Find("BasePlatform"); 
        BasePlatform basePlatform = thePlatform.GetComponent<BasePlatform>();
		
		//arrayLength = basePlatform.platfromArray;//.GetLength();
		Debug.Log(basePlatform.platfromArray);
		
	}

your int array variable should be like this int[ ] arrayLength

Thanks for the quick response. Should have seen that. Getting another weird error at line 34.

GameObject thePlatform = GameObject.Find("BasePlatform"); 
   //Below is line 34.
     BasePlatform basePlatform = thePlatform.GetComponent<BasePlatform>();

NullReferenceException
UnityEngine.GameObject.GetComponent[BasePlatform] () (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:18)
RandomSize.Start () (at Assets/My Assets Folder/Scripts/RandomSize.cs:34)

thePlatform is NULL e.g. not found by the lookup.

Yep that was the problem. Now i’ve got another. It didn’t like me using an int[ ] to contain it so I swapped to the GameObject[ ] one instead. The problem I have not is that I still can’t get the length of the array.

 Debug.Log(arrayLength.GetLength());

This doesn’t work. It comes up with two errors.

  1. The best overload method match for ‘Unity.Engine.Debug.Log(object)’ has some invalid arguments.

  2. Argument ‘#1’ cannot convert ‘method group’ expression top type ’ object’

Seems arrayLength.GetLength() doesn’t return an object.

I’m not sure if this will help. The reason I want to get the array is so i can cycle through the list of game objects creates in the BasePlatform script and apply changes to them.

using UnityEngine;
using System.Collections;

public class BasePlatform : MonoBehaviour {
	
	public int platformAmount = 1;
	public GameObject platform;	
	public GameObject[] platfromArray;

	// Use this for initialization
	void Start () {
		
		platfromArray = new GameObject[platformAmount];
		
		for(int i = 0; i < platformAmount; i++)
		{
			GameObject temp = Instantiate(platform) as GameObject;
			//temp.transform.tag = "Platform " + i;
			Debug.Log("Platform " + i);
			platfromArray[i] = temp;
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

The changes I want to make need to be different each time applied. So what I want to do is loop through array and retrieve/edit the spawned objects in the script below.

using UnityEngine;
using System.Collections;

public class RandomSize : MonoBehaviour {
	
	private float random_x;
	private float random_y;
	private float random_z;
	private float random_zS;
	private GameObject spawnRadius;
	public float LengthRadius = 1.5f;
	public float HeightRadius = 1.8f;
	private float spawnPosX;
	private float spawnPosY;
	private float spawnPosZ;
	GameObject []arrayLength;
	
	//public Transform blah;
	
		
	// Use this for initialization
	void Start () {
		
		random_z = Random.Range(-8, 8);
		random_x = Random.Range(7, 10);
		random_zS = Random.Range(4, 16);
		random_y = Random.Range(spawnPosY, spawnPosY+0.5f);
		spawnRadius = GameObject.FindWithTag("Spawn");
		spawnPosX = spawnRadius.transform.position.x;
		spawnPosY = spawnRadius.transform.position.y;
		spawnPosZ = spawnRadius.transform.position.z;
		
		GameObject thePlatform = GameObject.Find("Base Platform"); 
        BasePlatform basePlatform = thePlatform.GetComponent<BasePlatform>();
		
		arrayLength = basePlatform.platfromArray;
		Debug.Log(arrayLength.GetLength);
		
	}
	
	// Update is called once per frame
	void Update () {
		
		//Debug.Log(arrayLength);	
		
		transform.localScale = new Vector3(random_x, 1f, random_zS);
		
		transform.position = new Vector3(spawnPosX + random_x * LengthRadius, spawnPosY + random_y * HeightRadius+1.25f, spawnPosZ  + random_z * LengthRadius);
	
	}
	

	
}

Just use arrayLength.Length and not Array.GetLength(Int32) Method (System) | Microsoft Learn