Hi,
I have one parent object named “sphere”, this object has a child object called “cube”
I want that the cube rotate when the sphere moves, so I wrote a script on the cube called “follow” :
follow.cs :
public class follow : MonoBehaviour {
public float smooth = 20;
int flag = 0;
public void follow_sphere_right(){
if (flag != 1) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(transform.rotation.x,transform.rotation.y + 60,transform.rotation.z), smooth * Time.deltaTime);
flag = 1;
}
}
public void follow_sphere_left(){
if (flag != 2) {
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(transform.rotation.x,transform.rotation.y - 60,transform.rotation.z), smooth * Time.deltaTime);
flag = 2;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
and I want to call the two functions follow_sphere_right
and follow sphere_left
whenever the sphere moves using input keys
Here is the script I use on the sphere, sphere.cs:
public class SphereScr : MonoBehaviour {
float speed = 2;
follow other;
// Use this for initialization
void Start () {
other = gameObject.GetComponent<follow>();
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.K)) {
this.transform.Translate (speed * Time.deltaTime, 0f, 0f, Space.World);
other.follow_sphere_right();
}
if (Input.GetKey (KeyCode.L)) {
this.transform.Translate (speed * Time.deltaTime * -1, 0f, 0f, Space.World);
other.follow_sphere_left();
}
}
}
But the object “other” is always null : (NullReferenceException: Object reference not set to an instance of an object
)
thank you for helping