C# 一旦触发,如何更改整个对象的颜色?

C# 一旦触发,如何更改整个对象的颜色?,c#,unity3d,colors,triggers,snapping,C#,Unity3d,Colors,Triggers,Snapping,我正在做一个化学分子游戏。到目前为止,我有一个在受体位置上有触发器的分子,当我把正确的原子拖到这个位置附近时,原子会改变颜色并附着。一旦这个原子被连接,我如何改变整个分子的颜色(不仅仅是触发/受体部位) 我的游戏这一部分使用的代码来自: 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类快照:单一行为 { 公共字符串partnerTag; 公共浮球闭合距离=0.05f; 公共浮点数farVPDist=1; 公共浮子移动速度=40.0f

我正在做一个化学分子游戏。到目前为止,我有一个在受体位置上有触发器的分子,当我把正确的原子拖到这个位置附近时,原子会改变颜色并附着。一旦这个原子被连接,我如何改变整个分子的颜色(不仅仅是触发/受体部位)

我的游戏这一部分使用的代码来自:

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类快照:单一行为
{
公共字符串partnerTag;
公共浮球闭合距离=0.05f;
公共浮点数farVPDist=1;
公共浮子移动速度=40.0f;
公共浮球旋转速度=90.0f;
专用矢量3屏幕点;
专用矢量3偏移量;
私人住宅区;
颜色=新颜色(2,1,3);
浮点距离=数学无穷大;
正常颜色;
gameobjectpartnergo;
//用于初始化
void Start()
{
normalColor=GetComponent().material.color;
partnerGO=GameObject.FindGameObjectWithTag(partnerTag);
}
void OnMouseDown()
{
screenPoint=Camera.main.WorldToScreenPoint(transform.position);
offset=transform.position-Camera.main.ScreenToWorldPoint(新矢量3(Input.mousePosition.x,Input.mousePosition.y,screenPoint.z));
Cursor.visible=false;
}
void OnMouseDrag()
{
//transform.SetParent(null);
Vector3 curScreenPoint=新的Vector3(Input.mousePosition.x,Input.mousePosition.y,screenPoint.z);
Vector3 curPosition=Camera.main.ScreenToWorldPoint(curScreenPoint)+偏移量;
transform.position=curPosition;
Vector3 partnerPos=Camera.main.WorldToViewportPoint(partnerGO.transform.position);
Vector3 myPos=Camera.main.WorldToViewportPoint(transform.position);
距离=矢量2.距离(partnerPos,myPos);
GetComponent().material.color=(distfarVPDist)
{
//transform.SetParent(null);
}
}
IEnumerator InstallPart()
{
while(transform.localPosition!=Vector3.right | | transform.localRotation!=Quaternion.identity)
{
transform.localPosition=Vector3.moveToward(transform.localPosition,Vector3.right,Time.deltaTime*moveSpeed);
transform.localRotation=Quaternion.rotatetowwards(transform.localRotation,Quaternion.identity,Time.deltaTime*rotateSpeed);
返回新的WaitForEndOfFrame();
}
}
}

OnMouseUp
中,添加一行以更改分子的材质/颜色。代码:

public Color newColor;
public GameObject objectToChangeColour;    
void OnMouseUp()
{
    Cursor.visible = true;
    if (dist < closeVPDist)
    {
        transform.SetParent(partnerGO.transform);
        StartCoroutine(InstallPart());
        isSnaped = true;
        objectToChangeColour.GetComponent<Renderer>().material.color = newColour; // Change the color of object to the newColour
    }
    if (dist > farVPDist)
    {
        //  transform.SetParent(null);
    }
}
公共颜色newColor;
公共游戏对象反对改变颜色;
void OnMouseUp()
{
Cursor.visible=true;
如果(距离farVPDist)
{
//transform.SetParent(null);
}
}

只需将
objecttochangecolor
更改为您想要更改其颜色的对象(parentGO/molecular/等),并将
newcolor
更改为新颜色。

请发布您的代码以及您迄今为止为他人所做的尝试,以帮助您实现正确的效果!非常感谢你!
public Color newColor;
public GameObject objectToChangeColour;    
void OnMouseUp()
{
    Cursor.visible = true;
    if (dist < closeVPDist)
    {
        transform.SetParent(partnerGO.transform);
        StartCoroutine(InstallPart());
        isSnaped = true;
        objectToChangeColour.GetComponent<Renderer>().material.color = newColour; // Change the color of object to the newColour
    }
    if (dist > farVPDist)
    {
        //  transform.SetParent(null);
    }
}