Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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: 

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

[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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges