- Home /
C# Check if parent's script's GameObject variable equals GameObject
I'm trying to check if a parent's script's gameobject variable equals a gameobject. Void OnMouseDown works fine but the if statement never equals true for some reason. Void OnGUI is check every frame like update and there isn't anything preventing Void OnGUI itself from running. Any ideas?
void OnMouseDown() {
transform.parent.GetComponent<ParentScript>().curGO = gameObject;
}
void OnGUI(){
if(gameObject == transform.parent.GetComponent<ParentScript>().curGO){
GUI.Window(0, new Rect(Screen.width/2 + 200, Screen.height/2 - 275, 300, 550), WindowFunction, "Window Function");
}
}
It works fine for me, it must be a setup problem. Both of the scripts are enabled ? The parent object have a ParentScript attached to it and enabled ?
Answer by Ochreous · Jun 02, 2015 at 10:51 PM
I figured it out I was calling Void OnGUI from another script and it cancelled out the other(in this case my GO script).
Answer by alok-kr-029 · Jun 02, 2015 at 07:52 AM
if you want to compare two game objects compare it ids..... by using getinstanceid().... this is the best practice as every gameobject has an unique id
Your answer
![](https://koobas.hobune.stream/wayback/20220613200539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Debug.Log a gameobject in C ? 2 Answers
Distribute terrain in zones 3 Answers
Instantiated GameObject gets spawned as a child 2 Answers
"Center On Children" programmatically 1 Answer
Can one specify the parent of a gameobject in an array? 2 Answers