我有一組貼在我的遊戲對象的一個代碼編寫的腳本,我試圖在另一個對象從腳本訪問。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();
但它給我兩個錯誤:
MoveBackwards是一種類型,但像變量一樣使用
類型或命名空間名稱「移動」找不到(是否 缺少使用指令或程序集引用?
任何想法如何解決這個問題?
由於
沒有嘗試'GameObject.FindObjectOfType()'或'GameObject.FindGameObjectsWithTag( 「」)' –
這不聞C. –
不要垃圾郵件標籤。這顯然不是C. – Olaf