Search code examples
c#unity-game-enginegameobject

Accesing a script attached to another object


I have a set of code attached to one of my game objects, that I am trying to access from the script in another object.

I am trying to access the "Move" function in this script:

 using UnityEngine;
 using System.Collections;

 public class MoveBackwards : MonoBehaviour 
 {
     #region Inspector


     public float maxRotationDegrees = 10f;
     public float rotationSpeed = 2f;

     public float maxDistance = 5f;
     public float moveSpeed = 2f;

     #endregion //Inspector

     private float traveledDistance;
     private float rotatedAmount;
     private bool isMoving;

     #region Unity Engine & Events

     private void Update()
     {

         AudioSource audio = GetComponent<AudioSource>();

         if(isMoving)
         {
             if(traveledDistance < maxDistance)
             {
                 Vector3 moveDirection = -transform.up;
                 transform.position += moveDirection * moveSpeed * Time.deltaTime;
                 traveledDistance += Mathf.Abs(moveSpeed * Time.deltaTime);
             }
             if(rotatedAmount < maxRotationDegrees)
             {
                 transform.Rotate(0, 0, rotationSpeed * Time.deltaTime);
                 rotatedAmount += Mathf.Abs(rotationSpeed * Time.deltaTime);
             }
         }
     }

     #endregion //Unity Engine & Events

     public void Move()
     {

         isMoving = true;
     }

 }

Using this code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class PushbackAudio : MonoBehaviour {

     public AudioSource PushbackAudioPilot;
     public AudioSource PushbackApproved;
     public AudioSource PushbackApprovedPRB; //PRB = Pilot read back
     public bool running = true;

     public void AircraftPushbackAudioProcedure()
     {
         StartCoroutine(AircraftPushbackIEnumerator());
     }

    private IEnumerator AircraftPushbackIEnumerator()
     {
         running = true;

         while (running)
         {
             PushbackAudioPilot.Play();
             yield return new WaitForSeconds(7);
             PushbackApproved.Play();
             yield return new WaitForSeconds(5);
             PushbackApprovedPRB.Play();
             yield return new WaitForSeconds(5);
             MoveBackwards = GameObject.Find("Aircraft Sprite").GetComponent<Move>();


         }
     }
 }

I am using the line: MoveBackwards = GameObject.Find("Aircraft Sprite").GetComponent(); but it is giving me two errors:

  1. MoveBackwards is a type but is used like a variable

  2. The type or namespace name "Move" could not be found (are you missing a using a directive or an assembly reference?

Any idea how to fix this?

Thanks


Solution

  • I am using the line: MoveBackwards = GameObject.Find("Aircraft Sprite").GetComponent(); but it is giving me two errors:

    That should be:

    MoveBackwards moveBc = GameObject.Find("Aircraft Sprite").GetComponent<MoveBackwards>();

    You can then call the Move function:

    moveBc.Move();
    

    You only missed <MoveBackwards> before ().