简体   繁体   English

如何在Unity3d中使用相同的脚本为每个游戏对象设置不同的值变量

[英]How to set different value variables for each game object using same script in Unity3d

I am quite new into Unity and I am trying to create three different objects having one script and set a value variable for each instance. 我对Unity还是很陌生,我试图创建三个具有一个脚本的不同对象,并为每个实例设置一个变量。

I have drag and drop the script to three of the objects within my scene and set from the unity ui slots value variable to different values for each object! 我已将脚本拖放到场景中的三个对象上,并从unity ui插槽value变量设置为每个对象不同的值!

Current code looks like: 当前代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class DragBall : MonoBehaviour {

    public GameObject gameObjectToDrag;
    public Text txt;


    public int value;

    public Vector3 GOcenter;
    public Vector3 touchPosition;
    public Vector3 offset;
    public Vector3 newGOcenter;

    RaycastHit hit;

    public bool draggingMode = false;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0)) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                gameObjectToDrag = hit.collider.gameObject;
                GOcenter = gameObjectToDrag.transform.position;
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                offset = touchPosition - GOcenter;
                draggingMode = true;
            }
        }

        if (Input.GetMouseButton(0)) {
            if (draggingMode) {
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                newGOcenter = touchPosition - offset;
                gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 3, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
            }
        }

        if (Input.GetMouseButtonUp(0)) {
            draggingMode = false;
        }

        Debug.Log(this.value);
        txt.text = this.value + "";
    }
}

Edited Code: 编辑代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class DragBall : MonoBehaviour {

    public GameObject gameObjectToDrag;

    public Text txt;

    public Vector3 GOcenter;
    public Vector3 touchPosition;
    public Vector3 offset;
    public Vector3 newGOcenter;

    RaycastHit hit;

    public int value;

    public bool draggingMode = false;

    // Use this for initialization
    void Start () {
        if (this.gameObjectToDrag.name == "Ball1")
        {
            this.value = 1;
        }
        else if (this.gameObjectToDrag.name == "Ball2")
        {
            this.value = 2;
        }
        else if (this.gameObjectToDrag.name == "Ball3")
        {
            this.value = 3;
        }
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0)) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                this.gameObjectToDrag = hit.collider.gameObject;
                GOcenter = this.gameObjectToDrag.transform.position;
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                offset = touchPosition - GOcenter;
                draggingMode = true;
            }
        }

        if (Input.GetMouseButton(0)) {
            if (draggingMode) {
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                newGOcenter = touchPosition - offset;

                if (this.gameObjectToDrag.name == "Ball1") {
                    this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 1, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
                }
                else if (this.gameObjectToDrag.name == "Ball2")
                {
                    this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 2, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
                }
                else if (this.gameObjectToDrag.name == "Ball3")
                {
                    this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 3, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
                }

            }
            txt.text = this.value + "";
        }

        if (Input.GetMouseButtonUp(0)) {
            draggingMode = false;
        }
    }
}

I would like for each object I touch with mouse to show current value variable . 我希望为用鼠标触摸的每个对象显示当前值变量。 Right now is showing 0 for all. 现在,所有显示为0。

Is it that I should create different script for each object and assign the script to each object seperately? 是否应该为每个对象创建不同的脚本并将脚本分别分配给每个对象? that doesnt just sound correct way for me idk why, but there must be something! 这对我idk来说不仅仅听起来是正确的方法,但是必须有一些东西!

You didn't initialize the value variable so it becomes 0 when attached to the 3 Objects. 您没有初始化value变量,因此当附加到3个Object时它变为0 Even if you initialize it to a number, it will still be that on every instance. 即使将其初始化为数字,每个实例上仍将是该数字。

You need a way to initialize the value variable on each GameObject . 您需要一种方法来初始化每个GameObject上的value变量。 To do that, you also need a way to distinguish each one of the GameObject . 为此,您还需要一种区分每个GameObject This can be done by comparing the current name of the GameObject, the tag, the layer or the GameObject instance. 这可以通过比较GameObject的当前名称,标签,图层或GameObject实例来完成。

The example below uses the name of the GameObject to initialize the value variable in the Start function. 下面的示例使用GameObject的名称在Start函数中初始化value变量。 Let's say they are named "Obj1", "Obj2" and "Obj3": 假设它们分别命名为“ Obj1”,“ Obj2”和“ Obj3”:

void Start()
{
    //Use 1 for Object 1
    if (this.gameObject.name == "Obj1")
    {
        value = 1;
    }
    //Use 3 for Object 2
    else if (this.gameObject.name == "Obj2")
    {
        value = 2;
    }
    //Use 3 for Object 3
    else if (this.gameObject.name == "Obj3")
    {
        value = 3;
    }
}

If you set the value to a different number in the inspector this script should work and show a different number in your log. 如果您在检查器中将值设置为其他数字,则此脚本应该可以工作并在日志中显示其他数字。 What I can see is that you run the txt.text = this.value every update. 我看到的是您每次更新都运行txt.text = this.value If you have one text field connected to all instances of your ball it will always show the same value. 如果您有一个文本字段连接到球的所有实例,则它将始终显示相同的值。 If you want the text field to show the current ball being dragged you should put that line of code in your if statement like this: 如果要让文本字段显示当前被拖动的球,则应将该代码行放入if语句中,如下所示:

    if (Input.GetMouseButtonDown(0)) {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            //Add line here
            txt.text = this.value + "";
            gameObjectToDrag = hit.collider.gameObject;
            GOcenter = gameObjectToDrag.transform.position;
            touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            offset = touchPosition - GOcenter;
            draggingMode = true;
        }
    }

If you have this value set differently for each ball it should work 如果您为每个球设置了不同的值,那么它应该起作用

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM