2017-03-12 65 views
0

我有一組貼在我的遊戲對象的一個​​代碼編寫的腳本,我試圖在另一個對象從腳本訪問。Accesing連接到另一個對象

我試圖訪問腳本中的「移動」功能:

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


     } 
    } 
} 

我現在用的線:MoveBackwards = GameObject.Find("Aircraft Sprite").GetComponent();但它給我兩個錯誤:

  1. MoveBackwards是一種類型,但像變量一樣使用

  2. 類型或命名空間名稱「移動」找不到(是否 缺少使用指令或程序集引用?

任何想法如何解決這個問題?

由於

+0

沒有嘗試'GameObject.FindObjectOfType()'或'GameObject.FindGameObjectsWithTag( 「」)' –

+0

這不聞C. –

+0

不要垃圾郵件標籤。這顯然不是C. – Olaf

回答

1

我使用的行:。MoveBackwards = GameObject.Find( 「飛機 精靈」)GetComponent();但它給我兩個錯誤:

這應該是:

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

然後,您可以調用移動功能:

moveBc.Move(); 

你只錯過了<MoveBackwards>()之前。

+0

謝謝。完美的作品! –

+0

歡迎您! – Programmer

相關問題