Unity如何多个物体挂载同一个脚本?

Unity如何多个物体挂载同一个脚本?
当我的脚本挂载在一个物体上时,效果是正确的,但当脚本挂载在多于一个物体上时,就会出现这样亿些问题:
只有一个物体上面有图片;
且那个有图片的物体的图片也不会刷新,但可以移动;
将那个可以有图片的物体重新激活后,就有两个带图片的物体,但是其中的那个移动的物体的图片不会刷新,而另一个物体的图片却显示了那个移动的物体应该显示的图片

我的代码如下

using System;
using UnityEngine;

public class person : MonoBehaviour
{
    //定义参数
    public short hp;
    public Color skincolor;
    public Color haircolor;
    public short hairimgindex;
    public short clotheimgindex;
    public short clothe2imgindex;
    public short shoesimgindex;
    public short pantimgindex;
    public short personnum;
    //素材路径集
    string hairimgpaths = "image/personimg/hairimg/";
    string clotheimgpaths = "image/personimg/clotheimg/";
    string shoesimgpaths = "image/personimg/shoesimg/";
    string pantimgpaths = "image/personimg/pantimg/";
    string bodyimgpath = "image/personimg/bodyimg/";
    string faceimgpath = "image/personimg/faceimg/";
    string headimgpath = "image/personimg/headimg/";
    string lowbodypath = "image/personimg/lowbodyimg/";

    //定义成员变量
    GameObject faceobj;//定义object
    GameObject hairobj;
    GameObject headobj;
    GameObject clotheobj;
    GameObject clothe2obj;
    GameObject bodyobj;
    GameObject shoesobj;
    GameObject pantobj;
    GameObject lowbodyobj;

    short moveflag = 0;//定义其他变量
    public short movedir = 0;
    short faceact = 0;
    short bodyact = 0;
    short lowbodyact = 0;
    short actdir = 0;
    bool isimgchange = false;

    void OnEnable()
    {
        faceobj = GameObject.Find("head/面部");//初始化素材object
        hairobj = GameObject.Find("head/头发");
        headobj = GameObject.Find("head/肉头");
        clotheobj = GameObject.Find("body/衣服1");
        clothe2obj = GameObject.Find("body/衣服2");
        bodyobj = GameObject.Find("body/肉体");
        shoesobj = GameObject.Find("lowerbody/鞋子");
        pantobj = GameObject.Find("lowerbody/裤子");
        lowbodyobj = GameObject.Find("lowerbody/肉腿");
        Debug.Log(name + "created");
    }

    void Update()
    {

        if (hp <= 0)//死亡检测
        {
            if (hp <= 0)
            {
                gameObject.SetActive(false);
            }
        }
    }

    void FixedUpdate()
    {
        if (moveflag > 0)
        {
            if (movedir == 0) gameObject.GetComponent<Transform>().position += new Vector3(0, -4F, 0); //前
            if (movedir == 1) gameObject.GetComponent<Transform>().position += new Vector3(-4f, 0, 0); //左
            if (movedir == 2) gameObject.GetComponent<Transform>().position += new Vector3(4f, 0, 0); //右
            if (movedir == 3) gameObject.GetComponent<Transform>().position += new Vector3(0, 4f, 0); //后
            if (movedir == 4) gameObject.GetComponent<Transform>().position += new Vector3(-3f, -3f, 0);//前左
            if (movedir == 5) gameObject.GetComponent<Transform>().position += new Vector3(3f, -3f, 0);//前右
            if (movedir == 6) gameObject.GetComponent<Transform>().position += new Vector3(-3f, 3f, 0);//后左
            if (movedir == 7) gameObject.GetComponent<Transform>().position += new Vector3(3f, 3f, 0);//后右
            moveflag -= 1;
        }
        if (movedir <= 3)
        {
            actdir = movedir;
            isimgchange = true;
        }
        else
        {
            if (movedir == 4 || movedir == 6) actdir = 0;//向左前和右前移动时,动画朝前
            if (movedir == 6) actdir = 1;//向左后移动时,动画朝左
            if (movedir == 7) actdir = 2;//向右后移动时,动画朝右
            isimgchange = true;
        }
        if (isimgchange)
        {
            Debug.Log(this.name);
            isimgchange = false;
            faceobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(faceimgpath + Convert.ToString(actdir) + "." + Convert.ToString(faceact));
            hairobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(hairimgpaths + Convert.ToString(hairimgindex) + "/" + Convert.ToString(actdir));
            headobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(headimgpath + Convert.ToString(actdir));
            bodyobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(bodyimgpath + Convert.ToString(actdir) + "." + Convert.ToString(bodyact));
            clothe2obj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(clotheimgpaths + Convert.ToString(clothe2imgindex) + "/" + Convert.ToString(actdir) + "." + Convert.ToString(bodyact));
            clotheobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(clotheimgpaths + Convert.ToString(clotheimgindex) + "/" + Convert.ToString(actdir) + "." + Convert.ToString(bodyact));
            lowbodyobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(lowbodypath + Convert.ToString(actdir) + "." + Convert.ToString(lowbodyact));
            pantobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(pantimgpaths + Convert.ToString(pantimgindex) + "/" + Convert.ToString(actdir) + "." + Convert.ToString(lowbodyact));
            shoesobj.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(shoesimgpaths + Convert.ToString(shoesimgindex) + "/" + Convert.ToString(actdir) + "." + Convert.ToString(lowbodyact));
            if (skincolor.a == 0) skincolor = Color.white;
            headobj.GetComponent<SpriteRenderer>().color = skincolor;
            bodyobj.GetComponent<SpriteRenderer>().color = skincolor;
            lowbodyobj.GetComponent<SpriteRenderer>().color = skincolor;
            if (haircolor.a == 0) haircolor = Color.black;
            hairobj.GetComponent<SpriteRenderer>().color = haircolor;
        }
    }

    public void move(short dir, short step = 1)
    {
        if (moveflag <= 0)
        {
            moveflag = step;
            movedir = dir;
        }
    }
}

然后创建角色的代码如下:

public static void personCreat(string name, int[] imgs,Color skincol,Color haircol,short dir=0)
{
    GameObject a = Instantiate(Resources.Load("Precast/person")) as GameObject;
    a.GetComponent<person>().name = name;
    a.GetComponent<person>().skincolor = skincol;
    a.GetComponent<person>().haircolor = haircol;
    a.GetComponent<person>().hairimgindex = Convert.ToInt16(imgs[0]);
    a.GetComponent<person>().clotheimgindex = Convert.ToInt16(imgs[1]);
    a.GetComponent<person>().clothe2imgindex = Convert.ToInt16(imgs[2]);
    a.GetComponent<person>().shoesimgindex = Convert.ToInt16(imgs[3]);
    a.GetComponent<person>().pantimgindex = Convert.ToInt16(imgs[4]);
    a.GetComponent<person>().personnum = Convert.ToInt16(imgs[5]);
    a.SetActive(true);
}

玩家移动代码如下:

using UnityEngine;

public class player : MonoBehaviour
{
    private void OnEnable()
    {
    }

    void Update()
    {
        //玩家移动操作
        if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) { gameObject.GetComponent<person>().move(4); }//sa
        if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D)) { gameObject.GetComponent<person>().move(5); }//sd
        if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A)) { gameObject.GetComponent<person>().move(6); }//wa
        if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) { gameObject.GetComponent<person>().move(7); }//wd
        if (Input.GetKey(KeyCode.A)) { gameObject.GetComponent<person>().move(1); }//左
        if (Input.GetKey(KeyCode.S)) { gameObject.GetComponent<person>().move(0); }//前
        if (Input.GetKey(KeyCode.D)) { gameObject.GetComponent<person>().move(2); }//右
        if (Input.GetKey(KeyCode.W)) { gameObject.GetComponent<person>().move(3); }//后
        
    }
}

代码顺序如下

img

感谢各位的帮助

怎么挂载的?

你把全局变量改成局部变量试一下呢

【相关推荐】




如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^