访问附加到另一个对象的脚本
Accesing a script attached to another object
我有一组代码附加到我的一个游戏对象,我正试图从另一个对象中的脚本访问。
我正在尝试访问此脚本中的 "Move" 函数:
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 是一种类型,但像变量一样使用
找不到类型或名称空间名称 "Move"(您是
缺少使用指令或程序集引用?
知道如何解决这个问题吗?
谢谢
I am using the line: MoveBackwards = GameObject.Find("Aircraft
Sprite").GetComponent(); but it is giving me two errors:
应该是:
MoveBackwards moveBc = GameObject.Find("Aircraft Sprite").GetComponent<MoveBackwards>();
然后您可以调用移动函数:
moveBc.Move();
您只在 ()
之前错过了 <MoveBackwards>
。
我有一组代码附加到我的一个游戏对象,我正试图从另一个对象中的脚本访问。
我正在尝试访问此脚本中的 "Move" 函数:
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 是一种类型,但像变量一样使用
找不到类型或名称空间名称 "Move"(您是 缺少使用指令或程序集引用?
知道如何解决这个问题吗?
谢谢
I am using the line: MoveBackwards = GameObject.Find("Aircraft Sprite").GetComponent(); but it is giving me two errors:
应该是:
MoveBackwards moveBc = GameObject.Find("Aircraft Sprite").GetComponent<MoveBackwards>();
然后您可以调用移动函数:
moveBc.Move();
您只在 ()
之前错过了 <MoveBackwards>
。