C# 访问附加到另一个对象的脚本
我有一组代码附加到我的一个游戏对象上,我正试图从另一个对象中的脚本访问这些代码 我正在尝试访问此脚本中的“移动”功能:C# 访问附加到另一个对象的脚本,c#,unity3d,unity5,gameobject,C#,Unity3d,Unity5,Gameobject,我有一组代码附加到我的一个游戏对象上,我正试图从另一个对象中的脚本访问这些代码 我正在尝试访问此脚本中的“移动”功能: using UnityEngine; using System.Collections; public class MoveBackwards : MonoBehaviour { #region Inspector public float maxRotationDegrees = 10f; public float rotatio
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;
}
}
使用UnityEngine;
使用系统集合;
公共阶层:单一行为
{
#区域检查员
公共浮点数maxRotationDegrees=10f;
公共浮动旋转速度=2f;
公共浮动最大距离=5f;
公共浮动速度=2f;
#endregion//Inspector
私人浮动旅行距离;
私人浮动旋转安装;
私人住宅;
#区域统一引擎和事件
私有void更新()
{
AudioSource audio=GetComponent();
如果(正在移动)
{
if(移动距离<最大距离)
{
Vector3 moveDirection=-transform.up;
transform.position+=moveDirection*moveSpeed*Time.deltaTime;
移动距离+=数学绝对值(移动速度*时间增量);
}
if(旋转安装<最大旋转度)
{
transform.Rotate(0,0,rotationSpeed*Time.deltaTime);
rotatedAmount+=Mathf.Abs(旋转速度*时间增量);
}
}
}
#endregion//Unity引擎和事件
公开作废动议()
{
isMoving=真;
}
}
使用此代码:
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>();
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类PushbackAudio:单一行为{
公共音频源PushbackAudioPilot;
公共音频源推送批准;
公共音频源PushbackApprovedPRB;//PRB=导频回读
公共bool running=true;
公共无效飞机pushbackaudioprocedure()
{
Start例程(AircraftPushbackIEnumerator());
}
私人IEnumerator飞机后推IEnumerator()
{
运行=真;
(跑步时)
{
PushbackAudioPilot.Play();
返回新的等待秒(7);
Pushback已批准。Play();
产生返回新WaitForSeconds(5);
回推批准的PRB.Play();
产生返回新WaitForSeconds(5);
MoveBackwards=GameObject.Find(“飞机精灵”).GetComponent();
}
}
}
我使用的行是:MoveBackwards=GameObject.Find(“飞机精灵”).GetComponent()代码>但它给了我两个错误:
MoveBackwards是一种类型,但其用法与变量类似
找不到类型或命名空间名称“Move”(是否为“”)
缺少使用指令或程序集引用的
你知道怎么解决这个问题吗
谢谢
我正在使用一行代码:MoveBackwards=GameObject.Find(“飞机”)
Sprite”).GetComponent();但它给了我两个错误:
这应该是:
MoveBackwards-moveBc=GameObject.Find(“飞机精灵”).GetComponent();
然后可以调用Move函数:
moveBc.Move();
您在尝试GameObject.FindObjectOfType()
或GameObject.FindGameObjectsSwithTag(“”)之前,只错过了
,,,这没有C味道。不要垃圾标签。这显然不是C。