The question is answered, right answer was accepted
How to disable parent through its own child object.
I have a leveling representation on my game which uses a float of the player and compares it to another float. (basically, current experience compared to max experience of the current level). Once you reach the max float, an interger (the level) will increase with one.
Once you do this, a gameobject with 3 child objects will be enabled. these 3 child objects are 3 clickable sprites that serve as buttons.
(as the player, when you level up, ou get to choose between 3 upgrades, displayed as buttons)
What I want to do is allow any of the 3 buttons to disable it's parent, essentially disabling eachother so that you cannot reuse any of the buttons. in other words, once you press any of the 3 buttons, you cannot choose any other until you level up again.
Here's the onMouseUp functions of one of the buttons:
void OnMouseUp(){
hpIncrease.maxPlayerLives += 10;
hpIncrease.playerLives += 10;
level2.SetActiveRecursively(false);
}
level2 is a public gameobject which finds the tag of the parent object like this:
level2 = GameObject.FindGameObjectWithTag ("Level2");
This, however doesnt seem to work. It works only if the gameobject is already enabled when the game starts, but that's not what I want.
So in short: How do disable a gameobject through one of it's children, which in turn disables the children as well?
Answer by dkjunior · Oct 07, 2015 at 07:28 PM
You can do this by accessing parent's transform. The following code will do the job assuming it's in the script attached to one of your child objects:
void OnMouseUp() {
this.transform.parent.gameObject.SetActive(false);
}
Also, you don't need to disable all objects recursively, disabling only the parent object will do the job.
can't we disable the update function from the parent object.
Follow this Question
Related Questions
Does activating a parent activates children (MB's Awake()) 1 Answer
Selectively enable and disable Gamepads?,D 0 Answers
How to make Child hanging above rotating parent 0 Answers
SOLVED. How can I use one parent object animator to control multiple child sprite animations? 1 Answer
How can the pivot point of a object be changed during Runtime? 0 Answers