- Home /
How to Find gameObject child.
So I made a gameObject named camera then I dragged the main camera into the camera gameObject. So then In my code, I wanted to disable the script attached to the main camera named Camera Shaker, so I used this:
GameObject.Find("MainCamera").GetComponent<CameraShaker>().enabled = false;
This line of code help me with finding non Child gameObjects and disabling them, but since they are children gameObjects this won't work, I probably have to put the Child keyword in this line of code somewhere but I don't know how to do that. If possible I will like you to find a way so I can still use this simple line of code, and if not it anything will help.
Why use GameObject.Find("$$anonymous$$ainCamera")?
You could just create a public field...
public Camera mainCamera;
private void DeactivateShaker() {
mainCamera.gameObject.GetComponentInChildren<CameraShaker>().enabled = false;
}
...then drag the main camera into the editor for that field in the inspector for your script.
I try to avoid dynamically finding things whenever possible. Is there a reason you need to use GameObject.Find()?
Answer by DasNanda · Sep 29, 2017 at 07:32 AM
This should do the trick :)
this.GetComponentInChildren<CameraShaker>().enabled = false;
Just a coment.
when you use "this" in a script like
this.GetComponent<Button>();
You are refearing to the script, not the object containing the script. For refearing the object containig this script use
gameObject.GetComponent<Button>();
In some functions its the same using "this" and "gameObject" but it can give you problems in other functions.
Thank you, I've never had any problems with it, so I really overlooked that.
Answer by tormentoarmagedoom · Sep 29, 2017 at 10:55 AM
In your code, just replace GetComponent for GetComponentInChildren
@tormentoarmagedoom I need to mention some things, I have another gameObject named Player. I'm scripting this in the Player gameObject, so this may be why it's not working. Also I need to say that this line of code, GameObject.Find("").GetComponent<>(); can access children of gameObjects, but only when this script is attached to the parent of the child you are trying to access. For example, the Player gameObject has a child named 'Gun'. This Gun gameObject which is the child of the Player gameObject has a script named 'Bullet_Spawner'. Now I want to disable the Bullet_Spawner script in the Start $$anonymous$$ethod/Function, so I use this line of code:
GameObject.Find("$$anonymous$$ainCamera").GetComponent<CameraShaker>().enabled = false;
And it works.
Answer by davitsedrakian · Apr 17, 2020 at 10:54 PM
Hello, @DiamondMC102 you can just serialize the game object on what you need to turn on some scripts :
For Example on Player script you write
[SerializeField]
private Camera mainCamera;
after this drag and drop your main camera obejct into player object. After that in player script
mainCamera.GetComponent<CameraShaker>().enabled = false;
Your answer
Follow this Question
Related Questions
Don't Flip Child, Only Parent. 0 Answers
What happened to Line Renderer! 1 Answer
Flip Player, but don't flip the Player's Child. 2 Answers
How can I find gameObject child. 0 Answers
Flip Player, Not the Player's Child. 3 Answers