Using Child Object To Control Parent
I am trying to use one child object to control the parent. I have 9 cubes, all numbered using a Canvas and text on the cube primitives. The Cubes 1-4 and 6-9 are the children of an empty GameObject "Baby Cube." Cube 5 is the child of an empty game object "Master Cube." I want to use Cube 1 to make the parent Baby Cube rotate around the parent "Master Cube" so that when I click on Cube 1, parent Baby Cube (with all its children) rotates around Master Cube.
I have two scripts. Script A sets a bool for rotation.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RotateNewTwo : MonoBehaviour {
public Transform babyCube;
public bool isRotating = false;
// Update is called once per frame
void Update () {
if (isRotating){
transform.RotateAround(babyCube.transform.position, new Vector3(0, 0, -1), 20 * Time.deltaTime);
}
Script B gets the script component from the Baby Cube Component.
public class CubeOneControl : MonoBehaviour {
public GameObject babyCube;
public Transform CubeOne;
private RotateNewTwo rotateNewTwo;
private void Awake()
{
rotateNewTwo = babyCube.GetComponent<RotateNewTwo>();
CubeOne = this.gameObject.transform.GetChild(0);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
rotateNewTwo.isRotating = true;
}
}
When I click on Cube 1 the cubes do rotate but they also rotate if I click on any of the cubes. How do I get the gameobjects to only respond to the click on the child object?
Your answer
