- Home /
I need some help accessing an array from another script
I have been trying to figure out how to get information from the itemList class at the bottom of the code using another script, but I have not been able to wrap my head around the issue. Do any of you guys have any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class masterList : MonoBehaviour
{
public itemList[] itemLists = new itemList[5];
}
[System.Serializable]
public class itemList
{
public Transform[] prefabs = new Transform[5];
}
Answer by toddisarockstar · Jul 28, 2018 at 12:57 AM
if this is a "master list" and you only have one occurrence of your itemLists then why not just make it static?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class masterList : MonoBehaviour
{
public static itemList[] itemLists = new itemList[5];
}
[System.Serializable]
public class itemList
{
public Transform[] prefabs = new Transform[5];
}
then you can access it anywhere from any script by simply saying:
masterList.itemLists
alternatively you can use Getcomponent
GameObject go;
// go should be the game object the script is attached to
masterList myscript = go.GetComponent<masterList>();
// now access the script like this:
myscript.somevariable = whatever;
It worked! Thanks for helping me on this issue, you are a life saver!
Your answer
![](https://koobas.hobune.stream/wayback/20220612171705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Having trouble accessing a unique instance of a dynamically generated script... 0 Answers
Array of Quaternions based of Array of GameObjects (NOT WORKING) 1 Answer
how can i check if there is a blanc spot in my list 3 Answers
Spawn Point Repeating 3 Answers
An Instantiated object to array problem 2 Answers