Accesing a script attached to another object

风流意气都作罢 提交于 2019-12-12 00:52:43

问题


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


回答1:


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 ().



来源:https://stackoverflow.com/questions/42749082/accesing-a-script-attached-to-another-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!