Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by zcoldrick · Mar 06, 2014 at 03:23 PM · componentguitextaccessextend

Access parent variable in child script component.

I have created a class to "extend" the functionality of GUIText

My CustomGUIText class adds a few strings to represent on and off clicked states, the name of animation to be triggered by them and bool values to represent whether the text is in the on state or off state and whether it should be displayed or not.

Each CustomGUIText class is instantiated by another script that puts them all into a list, so they can be positioned where needed.

The mouseover event is handled by a third script that changes the text color when moused over.

When the text is clicked I would like it to call the ToggleToggle() function. This should change the toggle state and either start or stop the animation and change the name appropriately.

I can't work out how to access the parent variable from this child component script however.

Any ideas?

 using UnityEngine;
 using System.Collections;
 
 public class CustomGUIText {
 
     private string onText;
     private string offText;
     private string animName;
     private bool toggle;
     private bool display;
     private GameObject myGUIText;
 
     // Constructors
     public CustomGUIText(){
         onText = "";
         offText = "";
         animName = "";
         toggle = false;
         display = false;
         myGUIText = new GameObject();
         myGUIText.AddComponent (typeof(GUIText));
         myGUIText.guiText.enabled = false;
         myGUIText.AddComponent (typeof(OnMouseOverText));
         }
 
     public CustomGUIText(string onValue, string offValue,string animNameValue, bool toggleValue, bool show){
         onText = onValue;
         offText = offValue;
         animName = animNameValue;
         toggle = toggleValue;
         display = show;
         myGUIText = new GameObject();
         myGUIText.AddComponent (typeof(GUIText));
         myGUIText.AddComponent (typeof(OnMouseOverText));
         myGUIText.guiText.name = animName;
         if (toggle) {
                         myGUIText.guiText.text = onText;
         } else {myGUIText.guiText.text = offText;
                 }
         if (!display) {
             myGUIText.guiText.enabled = display;
                 }
         }
 
 
 
     // Getters
 
     public string GetOnText(){
         return onText;}
     public string GetOffText(){
                 return offText;
         }
     public string GetAnimName(){
                 return animName;
         }
     public bool GetToggle(){
                 return toggle;
         }
     public bool GetDisplay(){
                 return display;
         }
 
     // Setters
 
     public void SetOnText(string onValue){
                 onText = onValue;
         }
     public void SetOffText(string offValue){
                 offText = offValue;
         }
     public void SetAnimName (string animNameValue){
                 animName = animNameValue;
         }
     public void SetToggle (bool toggleValue){
                 toggle = toggleValue;
         }
     public void SetDisplay (bool displayValue){
                 display = displayValue;
         }
 
     // Toggles
 
     public void ToggleDisplay(){
                 display = !display;
         }
 
     public void ToggleToggle(){
         toggle = !toggle;
         }
 
     // setTextLocation
 
     public void SetTextLocation(Vector3 target) {
         myGUIText.transform.position = target;
     }
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (toggle) {
             myGUIText.guiText.text = onText;
         } else {myGUIText.guiText.text = offText;
         }
         if (!display) {
             myGUIText.guiText.enabled = display;
         }
     }
 
 
 }

// CreateTextPrefabs attaches to empty gameobject to create CustomGUITexts

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CreateTextPrefabs : MonoBehaviour {
     private List<CustomGUIText> myCustomGUITextArray;
     // Use this for initialization
     void Start () {
     // Here we create a series of CustomGUIText objects
 
         myCustomGUITextArray = new List<CustomGUIText>();
         myCustomGUITextArray.Add(new CustomGUIText ("On1", "Off1", "Anim1", false, true));
         myCustomGUITextArray.Add(new CustomGUIText("On2", "Off2", "Anim2", false, true));
         myCustomGUITextArray.Add (new CustomGUIText ("On3", "Off3", "Anim3", false, false));
         myCustomGUITextArray.Add (new CustomGUIText ("On4", "Off4", "Anim4", true, true));
 
         // put in location
         int index = 0;
         foreach (CustomGUIText item in myCustomGUITextArray) {
             if (item.GetDisplay()){
                 index++;
                 float yValue = 0.5f + (0.04f * index);
 
                 item.SetTextLocation(new Vector3(0.5f,yValue,0f));
             }
 
         }
 
     }
 
 }

// The problem is accessing the parent of this component script for mouse click events to toggle the bool parameters.

 using UnityEngine;
 using System.Collections;
 
 public class OnMouseOverText : MonoBehaviour {
 
     private CustomGUIText myCustomGUIText;
     // Use this for initialization
     void Start () {
         myCustomGUIText = GetComponent<CustomGUIText>(); // Doesn't like this
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnMouseEnter() {
         this.guiText.material.color = Color.red;
     }
     
     void OnMouseExit(){
         this.guiText.material.color = Color.white;
     }
 
     void OnMouseDown() {
         myCustomGUIText.ToggleToggle(); // 
         print ("Button Pressed");
     }
 
 }
Comment
Add comment · Show 2
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
avatar image Calum-McManus · Mar 06, 2014 at 03:43 PM 0
Share

Do you mean you want to get a script from parent object then use it in the script that is on the child object? If so have you tried:

 Script parentScript = transform.parent.GetComponent<Script>();

this use parentScript to access any public function or variable from then script

avatar image zcoldrick · Mar 06, 2014 at 04:08 PM 0
Share

It is worth noting that the void Start() and void Update() functions are NOT called in the CustomGUIText class because this does not implement $$anonymous$$onoBehavoir. The Update() function was removed after I understood this.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zcoldrick · Mar 06, 2014 at 04:12 PM

I solved this using the following:

Adding a get method for my gameobject in the customTextGUIscript

 public GameObject GetGameObject(){
     return myGUIText;
     }

Then adding a get method to access the array of customTextGUI objects in the globalscript object:

 // getter for the myCustomGUITextArray
 public List<CustomGUIText> GetCustomGUIList() {
     return myCustomGUITextArray;
 }


Finally, having the child script find the empty gameobject containing the List of CustomGUIText objects, the getting a reference to the script containing the get GetCustomGUIList() method. Then when the OnMouseDown function is called this gets the list of CustomTextGUI objects and checks each one to see if it's text is the same as the text of its attached parent (the one that was clicked). If it is: it accesses the gameobject within the CustomTextGUI with the GUIText compoent attached and sets its value appropriately.

 using System.Collections.Generic;
 
 public class OnMouseOverText : MonoBehaviour {
 
     private GameObject globalScriptContainer;
     private CreateTextPrefabs myCreateTextPrefabs;
     // Use this for initialization
 
 
     void Start () {
         globalScriptContainer = GameObject.Find ("_GlobalScripts");
         myCreateTextPrefabs = globalScriptContainer.GetComponent<CreateTextPrefabs>(); 
     }
 
 
     void OnMouseEnter() {
         this.guiText.material.color = Color.red;
     }
     
     void OnMouseExit(){
         this.guiText.material.color = Color.white;
     }
 
     void OnMouseDown() {
         List<CustomGUIText> myList = myCreateTextPrefabs.GetCustomGUIList ();
         foreach (CustomGUIText item in myList) {
             if (this.guiText.text.Equals(item.GetGameObject().guiText.text))
             {
             item.ToggleToggle();
                 if(item.GetToggle()){
             item.GetGameObject().guiText.text = item.GetOnText();
                 }
                 else
                 {item.GetGameObject().guiText.text = item.GetOffText();}
             }
                 }
 
         print ("Button Pressed");
     }
 
 }

Really complicated but it works: Clicking on the GUIText on the screen toggles it from the on state to off or vice versa and sets the toggle parameter to either true or false. This parameter can then be used to set a mechanim bool value with the string name specified by the animName parameter (not yet implemented).

Comment
Add comment · Share
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
avatar image
0

Answer by huntsfromshadow · Mar 06, 2014 at 03:54 PM

Okay yes the problem is any Component must extend from MonoBehavior. So you need to make your class definition for MyCustomGui to

 public class MyCustomGUI : Monobehavior {

Also to be clear you I think are using extend and parent wrong as those mean very specific things in OO and c# programming. Your question initiall confused me as you were using the words parent and extend in a different context.

Here is an example of how parent and extend mean in c# and OO programming.

     class Vehicle : Monobehaivor {
     //This is the parent class. It is a child of Monobehavior but from my code I'm calling it the parent
     
     private string var1;    //My childen will not see this as I'm private
     protected string var2; //My childen will see this as I'm protected
     public string var3; //The entire world can see this as I'm public
     
     //Functions work the same with with the private, protected, and public rules
     
     }
     
     class Car : Vehicle {
 
     /* I am a child of Vehicle, aka I extend vehicle.  I am also the parent of Sedan */
 
     /* From here I can see all of my parent's public and 
     protected methods */
     
     }
 
    class Sedan : Car {
       //I am a child of Car aka I extend car
   }
Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Accessing a variable of a script from another scene. 7 Answers

Declare Component type in GetComponent 2 Answers

Access guiText from other object. 3 Answers

Extending MonoBehaviour to share code between Objects 1 Answer

Very simple component-access question 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