C# - Accessing a public list in another script

Hey all,

I’m trying to create a list of textures to reference in my program, but I want to set it up in its own script to keep everything tidy. This list needs to be accessible from other scripts since it stores the textures. When I go to reference the list in other scripts it won’t auto populate in the drop down window that appears when you’re typing commands. Please note: the TerrainTextures class is much larger in the script, but I left one example in case it had any affect on it.

Here’s the script where I’m creating the list, with lots of unnecessary parts left out.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TerrainTextures {
	public Texture tex0S;
	
	public TerrainTextures(Texture ref0S) {
		tex0S = ref0S;
	}
}

public class TerrainTextureSetup : MonoBehaviour {
	
	public List<TerrainTextures> terrainTextureArray = new List<TerrainTextures>();
	public Texture dirt0S;
	
	// Use this for initialization
	void Start () {
		terrainTextureArray.Add(new TerrainTextures(dirt0S));
	}
}

Any assistance you can provide is greatly appreciated.

how do you access these?
If you want to see the TerrainTextures in the editor i think you have to “[System.serializable]” the TerrainTextures class.

terrainTextureArray[×].texOS ← this doesn’t work?

Hey ValooFX, thanks for the quick response.

I’m okay with not seeing TerrainTextures in the editor, I just want to use terrainTextureArray in other scripts besides the one it’s created in above. In the script above I’m able to use terrainTextureArray with no problem (it’ll auto populate when I start typing), but in any other script when I type it I just get a message that says “No matches” in the drop down box (the one that gives you context sensitive commands to help narrow down which one you’re wanting to use).

I’m not sure that my explanation is all that clear, so if not just let me know.

and where exactly does it have no matches, when accessing the array?

otherObject.GetComponent().terrainTextureArray[×].texOS

Guess you might use the singleton pattern here. Do the following in your TerrainTextureSetup script (by the way, I think the name is quite misleading):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class TerrainTextures {
	public Texture tex0S;
   
	public TerrainTextures(Texture ref0S) {
		tex0S = ref0S;
	}
}
 
public class TerrainTextureSetup : MonoBehaviour {

	public static TerrainTextureSetup tts;	// <-- creates a static variable
   
	public List<TerrainTextures> terrainTextureArray = new List<TerrainTextures>();
	public Texture dirt0S;
   
	// Use this for initialization
	void Start () {
		TerrainTextureSetup.tts = (TerrainTextureSetup)this;	// <-- assign here
		terrainTextureArray.Add(new TerrainTextures(dirt0S));
	}
}

Then in any other script you may call the TerrainTextureSetup by chaining it together like this: TerrainTextureSetup.tts., like:

TerrainTextures currentTexture = TerrainTextureSetup.tts.terrainTextureArray[0];

You could make it static

public class TerrainTextureSetup : MonoBehaviour {

    

    public static List<TerrainTextures> TerrainTextureArray = new List<TerrainTextures>();

    public Texture dirt0S;

    

    // Use this for initialization

    void Start () {

        TerrainTextureArray.Add(new TerrainTextures(dirt0S));

    }

}

so you can access it like;
TerrainTextureSetup.TerrainTextureArray

or you GetComponent to refrence it (which is standard unity behaviour you should be familiar with.

In general that should work as well, however when using the singleton pattern you might access more variables from the TerrainTextureSetup without having to make them all static. Shouldn’t make too much difference in the end though.

Regarding your singleton pattern, i find this a bit better but can cause issues if you rely on a singleton in Awake or OnEnable.

    private static TerrainTextureSetup _Instance;

    public static TerrainTextureSetup Instance {
        get { return _Instance; }
    }

    void Awake(){
        if(Instance != null  Instance != this)
        {
            Destroy(gameObject);
        }

        Instance = this;
    }

Thanks for all the replies. I’m at work right now so I haven’t had a chance to try out the different ideas here, but I definitely will when I get home.

Hah, that might be part of the problem! I’m still very new to Unity and programming in general; just learning as I go I guess. I’m not familiar with using GetComponent but I did a little reading and will see what I can figure out when I get home. It seems like there are a few options that have been thrown around here, but is there any single method that’s preferred?

It all depends on the situation, i have a mix of singletons, statics and getcomponents.

Alright, so I decided to go with the static approach since it seemed the most strait forward. I looked into GetComponent but I’m still having trouble wrapping my mind around how to apply it to arrays, so I’ll have to revisit that another day.

Appreciate the help everyone!

If the array is public, you can reach it easily with GetComponent<>.

GetComponent<TerrainTextureSetup>().terrainTextureArray

Doh, that’s stupid easy. Thank you :slight_smile: