- Home /
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");
}
}
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
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.
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).
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
}