Enabling/Disabling a Child GameObject from Parent Object Script in C#

Seems very basic but I can’t find a solution that works. I am a beginner and maybe I just don’t understand the ones that I’ve been looking up so I’m hoping someone could edit my Code so that it works.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
	public float xMin, xMax, zMin, zMax;
}

public class PlayerMovement : MonoBehaviour
{
	public GameObject Right;
	public float speed;
	public Boundary boundary;

	void Update () 
	{
		float moveHorizontal = Input.GetAxisRaw ("Horizontal");
		float moveVertical = Input.GetAxisRaw ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
		GetComponent<Rigidbody> ().velocity = movement * speed * Time.deltaTime;

		GetComponent<Rigidbody>().position = new Vector3
			(
				Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
				0.0f,
				Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
			);
		if (moveHorizontal > 0)
		{
			Debug.Log("Right");
   		}    
	}
}

So this code is attacked to the “Player” GameObject

alt text

I can move my Player Object around within a set Border. The Debug.Log also works, everytime I move right, i get a message. Now I want to be able to Activate the “Right” Child GameObject to be activated when I move right, what do I need to do here.

You want the GetComponentInChildren method:

Right rightScript = GetComponentInChildren<Right>();
rightScript.yourFunction();

This gives you access to your child object’s script components so you can run their functions as you need to.