简体   繁体   中英

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.

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!

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.

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!

You didn't initialize the value variable so it becomes 0 when attached to the 3 Objects. 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 . To do that, you also need a way to distinguish each one of the GameObject . This can be done by comparing the current name of the GameObject, the tag, the layer or the GameObject instance.

The example below uses the name of the GameObject to initialize the value variable in the Start function. Let's say they are named "Obj1", "Obj2" and "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. 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 (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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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