Solved. Question was too convoluted. Needed to look elsewhere.
Referencing Base Class In Prefab
So I am having an issue with referencing members in a base class through instantiated prefabs. Let me lay this out by just starting with the code...
Base Class:
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
public class CanvasMenuBase : MonoBehaviour
{
public GameObject canvas;
public CanvasMasterController masterController;
public DirectorConfiguration configDirector;
public string filePath;
public Dictionary<string, GameObject> go = new Dictionary<string, GameObject>();
public GameObject[] goArray;
public virtual void Start()
{
appendToStartFront();
setFilePath();
setReferences();
instantiateObjects();
enableObjects();
appendToStartEnd();
}
/// <summary>
/// For appending additional functionality to the start or end of the start function
/// </summary>
protected virtual void appendToStartFront(){}
protected virtual void appendToStartEnd(){}
/// <summary>
/// Function is setting the file path to assets needed for loading the menu in the
/// resources folder.
/// Format: TypeFolder/Sub-Folder/
/// </summary>
protected virtual void setFilePath(){}
protected virtual void setReferences()
{
configDirector = GameObject.Find("DirectorConfig").
GetComponent<DirectorConfiguration>();
canvas = GameObject.Find("Canvas");
masterController = canvas.GetComponent<CanvasMasterController>();
transform.SetParent(canvas.transform, false);
}
protected virtual void instantiateObjects()
{
for (int i = 0; i < goArray.Length; i++)
{
if (Convert.ToBoolean(configDirector.getData(goArray[i].name)))
{
// Instantiate the object, adding the instance to the Game Object dictionary
go.Add(goArray[i].name, (GameObject)Instantiate(Resources.Load(filePath + goArray[i].name)));
// Set parent of the object to this (menu object)
go[goArray[i].name].transform.SetParent(gameObject.transform, false);
}
}
}
protected virtual void enableObjects()
{
for (int i = 0; i < go.Count; i++)
{
if (go[goArray[i].name] != null && Convert.ToBoolean(configDirector.getData(goArray[i].name + "_Active")))
{
go[goArray[i].name].SetActive(true);
}
}
}
protected void initializeButtons()
{
for (int i = 0; i < go.Count; i++)
{
if (go[goArray[i].name] != null &&
go[goArray[i].name].GetComponent<Button>() != null)
{
go[goArray[i].name].GetComponent<Button>().interactable = Convert.ToBoolean(configDirector.getData(goArray[i].name + "_Interact"));
}
}
}
}
Derived Class:
using UnityEngine;
public class CanvasKeyController : CanvasMenuBase
{
protected override void setFilePath()
{
filePath = "GUI/MenuKeys/";
}
protected override void appendToStartEnd()
{
initializeButtons();
}
public void setNoKey()
{
Vector2 newPos = new Vector3(-75, -15);
updateGraphic(newPos);
}
public void setGreenKey()
{
Vector2 newPos = new Vector3(-75, -45);
}
public void setRedKey()
{
Vector2 newPos = new Vector3(-75, -75);
}
public void updateGraphic(Vector3 newPos)
{
// Snip
print(go.Count);
}
}
Everything here is instantiated through prefabs at run-time.
At run-time, the Canvas, which is already in the scene and uses a script that derives from CanvasMenuBase, instantiates Menu_Keys, which then instantiates all the objects stored in its script, which derives from the same base class. Currently, when I click NoKey, it reports go(the dictionary) as being empty.
The problem is that when accessing the dictionary in the base class, it accesses the dictionary associated with the original script (which is empty) rather than its own instance's base class. What would be the workaround for this? This approach of instantiating everything at run time is an unusual approach that I haven't used before, so if there's something obvious I'm missing or more information is needed, let me know.
Follow this Question
Related Questions
Only spawning power ups that the player wants in that game 1 Answer
[Editor] Find component for prefab from PrefabUtility 0 Answers
Null Reference Exception on Sprite Renderer 0 Answers
GUI.Window from child class not reacting 1 Answer
Instantiation of my GameObjects spell (from other script) 0 Answers