• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 21, 2016 at 05:19 PM by Free_Radical for the following reason:

Solved. Question was too convoluted. Needed to look elsewhere.

avatar image
0
Question by Free_Radical · Jan 19, 2016 at 05:13 PM · c#prefabsinheritanceinstantiationreferencing

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Only spawning power ups that the player wants in that game 1 Answer

Spawning prefabs randomly within a rectangle 1 Answer

Advance Colision Detection 1 Answer

Getting Parent GameObject from Child? 1 Answer

Saving/loading inherited class scripts (C#) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges