0

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!

2 Answers 2

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

I added exactly how you said, still not working, I am now doubting if I have setup correctly the script into each obejct, well what I did I just drag and drop the script file to each obejct and assing each object to gameObjectToDrag and set value variable different for each object, still no difference
This is very simple thing to do. Why not take screesnhots of the each of the 3 gameobjects then post the links here in the comment section. I want to take a look at it. Also take another screenshot of the Console tab log that shows it is still showing 0. Finally, update your question and EDIT under it then include your new code that shows your implementation from my answer. Do not remove your original code. Just add EDIT + your new code. You are doing something wrong but I need these to figure out what it is.
I think you asnwer is right, I just had to move your code into update() method so now I can see the text label is changing to a value depending on which object I am dragging! I will check your answer as correct! Thanks for helping on this1
It's better in the Start function otherwise you will run into issues when you try to modify it later on because the Update function will keep running. That code is supposed to run once only. Glad you got it working!
You don't have to create new question unless you have more questions or this comment I am about to make did not solve it. Enclose the three similar if (this.gameObjectToDrag.name == "Ball1") { this.value = 1; } statements with if(this.gameObjectToDrag != null). This makes sure gameObjectToDrag is not null before using this.gameObjectToDrag.name....
|
0

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

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.