C# 如何统一控制其他对象?
我使用c#for Unity,我有两个对象(当前一个是包含脚本文件的对象,另一个是我想更改其材质的对象),这是我的代码:C# 如何统一控制其他对象?,c#,unity3d,unity5,C#,Unity3d,Unity5,我使用c#for Unity,我有两个对象(当前一个是包含脚本文件的对象,另一个是我想更改其材质的对象),这是我的代码: public class PlayerController : MonoBehaviour { public Material[] material; Renderer rend; public float speed; private Rigidbody rb; void Start () { rend = GetComponent<Renderer&
public class PlayerController : MonoBehaviour {
public Material[] material;
Renderer rend;
public float speed;
private Rigidbody rb;
void Start ()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material [0];
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{ // Here is the problem, it will change the color of the current object not the other one
rend.sharedMaterial = material [1];
}
}
}
公共类PlayerController:MonoBehavior{
公共材料[]材料;
渲染器渲染;
公众浮标速度;
私人刚体;
无效开始()
{
rend=GetComponent();
rend.enabled=true;
rend.sharedMaterial=物料[0];
rb=GetComponent();
}
无效固定更新()
{
float moveHorizontal=Input.GetAxis(“水平”);
float moveVertical=Input.GetAxis(“垂直”);
Vector3移动=新Vector3(水平移动,0.0f,垂直移动);
rb.AddForce(移动*速度);
}
无效对撞机(对撞机其他)
{
if(other.gameObject.CompareTag(“拾取”))
{//问题是,它将更改当前对象的颜色,而不是另一个对象的颜色
rend.sharedMaterial=物料[1];
}
}
}
请帮忙!
谢谢大家您的rend对象是在start方法中设置的。我认为您需要获得另一个游戏对象,如:
if (other.gameObject.CompareTag ( "Pick Up"))
{
var changeColorObject = other.GetComponent<Renderer>();
changeColorObject.sharedMaterial = material [1];
}
if(other.gameObject.CompareTag(“拾取”))
{
var changeColorObject=other.GetComponent();
changeColorObject.sharedMaterial=材料[1];
}
您的rend对象是在start方法中设置的。我认为您需要获得另一个游戏对象,如:
if (other.gameObject.CompareTag ( "Pick Up"))
{
var changeColorObject = other.GetComponent<Renderer>();
changeColorObject.sharedMaterial = material [1];
}
if(other.gameObject.CompareTag(“拾取”))
{
var changeColorObject=other.GetComponent();
changeColorObject.sharedMaterial=材料[1];
}
您需要在另一个变量上使用以访问,然后才能访问其
void ontriggenter(碰撞器其他)
{
if(other.gameObject.CompareTag(“拾取”))
{
//获取渲染器或网格渲染器
Renderer otherRenderer=other.GetComponent();
otherRenderer.sharedMaterial=材质[1];
}
}
您需要在另一个变量上使用以访问,然后才能访问其
void ontriggenter(碰撞器其他)
{
if(other.gameObject.CompareTag(“拾取”))
{
//获取渲染器或网格渲染器
Renderer otherRenderer=other.GetComponent();
otherRenderer.sharedMaterial=材质[1];
}
}
非常感谢您!非常感谢你!