问题描述
我有一组代码附加到我的一个游戏对象上,我正尝试从另一个对象中的脚本进行访问.
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 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();
,但它给了我两个错误:
I am using the line: MoveBackwards = GameObject.Find("Aircraft Sprite").GetComponent();
but it is giving me two errors:
-
MoveBackwards是一种类型,但像变量一样使用
MoveBackwards is a type but is used like a variable
找不到类型或名称空间名称"Move"(您是缺少使用指令或程序集引用吗?
The type or namespace name "Move" could not be found (are youmissing a using a directive or an assembly reference?
有什么办法解决这个问题吗?
Any idea how to fix this?
谢谢
推荐答案
应该是:
MoveBackwards moveBc = GameObject.Find("Aircraft Sprite").GetComponent<MoveBackwards>();
然后您可以调用Move函数:
You can then call the Move function:
moveBc.Move();
您只错过了()
之前的<MoveBackwards>
.
这篇关于访问附加到另一个对象的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!