Dynamically Assign GameObject variable

I am trying to assign a GameObject variable as an instantiated GameObject is clicked on. It has a box collider and a rigid body, and everything seems to be working. I can get the name of the object clicked, but I can’t seem to be able to set the myObject GameObject to the object I’m getting the name of…

Any help is much appreciated.

Here’s the code:

Public GameObject myObject;

private void OnMouseDown()
    {
        Ray objectRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit objectHit;
        if (Physics.Raycast(objectRay, out objectHit))
        {
            if (objectHit.collider != null)
            {
                GameObject touchedObject = objectHit.transform.gameObject;
                string myObjectName = touchedObject.transform.parent.name;
              
               // I get the name of the parent object here.
               //Debug.Log("YES! You clicked " + touchedObject.transform.parent.name);

                myObject = (GameObject)GameObject.Find(myObjectName);

                if (touchedObject.tag == "InventoryItem")
                {
                    invItemCanvas.enabled=(true);
                  
                }
            }
        }
    }

Not sure why you want to use GameObject.Find when you already have the object.

Just set it = to the touchedObject. Or better yet, just set it to the objectHit.transform.gameObject

myObject = objectHit.transform.gameObject;

Also looking, you’re getting the name of the parent, you can also get the parent gameObject instead if you want

myObject = objectHit.transform.parent.gameObject;
1 Like

I’ve tried it, and even just now went back and changed the code again to make sure…

private void OnMouseDown()
    {
        Ray objectRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit objectHit;
        if (Physics.Raycast(objectRay, out objectHit))
        {
            if (objectHit.collider != null)
            {
                //GameObject touchedObject = objectHit.transform.gameObject;
                myObject=objectHit.transform.gameObject;
                string myObjectName = myObject.name;
               
                Debug.Log("YES! You clicked " + myObject.name);
                myObject = (GameObject)GameObject.Find(myObjectName);

                if (myObject.tag == "InventoryItem")
                {
                    invItemCanvas.enabled=(true);
                   
                }
            }
        }
    }

I get the names without any issue, but what is not happening is the GameObject myObject variable is not getting assigned…meaning in the inspector, the variable My Object is still “none (GameObject)”

Are you 100% sure that the object you are looking at in the inspector is the same one that is executing your code? Maybe you have more than one copy of that class in your scene.

If you pass a GameObject as the second parameter to Debug.Log, then clicking that log message in the console will show you in the hierarchy which object it came from. e.g.
Debug.Log("Some message", gameObject);

yes. That’s where the "Debug.Log(“YES! You clicked " + myObject.name);” part came in. I added in the “YES” part to verify. I should be able to set the variable to the object clicked on or it’s parent, right?

Added the Debug.Log("Some message", myObject.transform.parent);

It hilights the Game Object I want the variable to be as expected, but I can’t seem to set the variable in the script to that object.

You seem to be missing my point. I am saying you should write
Debug.Log("YES! You clicked " + myObject.name, gameObject);
with the last parameter being exactly “gameObject”. NOT “myObject” or any derivative thereof.

The point of this is that when you select that line in the console, you will see the object that wrote the message. The one that is executing your OnMouseDown function, and that contains the “myObject” variable whose value you just assigned. (NOT the one that you clicked on and tried to store inside of the “myObject” variable.)

I bet you will discover it is NOT the same object that you are currently looking at in the inspector to check whether the variable got assigned. (Because clearly the variable is getting assigned, or otherwise “myObject.name” would throw a null reference exception instead of printing the name that you are expecting.)

Though another possibility is that maybe you are assigning the correct variable, but then some other piece of code is immediately overwriting the same variable with a null value.

1 Like

You are correct. The prefab I was instantiating had the same script on it, so now, after removing the script, nothing is happening. I’ll have to scratch my head about this, as I want to use the same buttons to control the “current” selected game object.

Sounds like the object with the variable you are trying to update is NOT running this script–either the script is not attached, or the component is disabled, or the game object is inactive, or something like that.

That was the case. I’m instantiating a prefab that has the script on it that enables the canvas when the prefab is clicked. Then I’m using the canvas components to manipulate the prefab. Basically buttons to move it around. So, I know now that I need to separate out the script a bit, and call a script on the canvas to populate the data for the buttons. I’ll post that approach here once it’s done. I haven’t done this much, but basically, I’ll be passing some variables from the prefab to the script on the canvas.

This is working now… Here’s the hack… I have the same script, for convenience, on both my prefab, and the inventory control game object. By accessing the control object component in the script, I can set the variable I need. Hacky for sure, but it is working.

 private void OnMouseDown()
    {
        Ray objectRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit objectHit;
        if (Physics.Raycast(objectRay, out objectHit))
        {
            if (objectHit.collider != null)
            {
                //GameObject touchedObject = objectHit.transform.gameObject;
                myObject = objectHit.transform.gameObject;

                Debug.Log("Some message", gameObject);
                //Debug.Log("YES! You clicked " + myObject.name);
                // myObject = (GameObject)GameObject.Find(myObjectName);

                invCntrlObject.GetComponent<InventoryCanvasSwitch>().SetInvItem(myObject);

                if (myObject.tag == "InventoryItem")
                {
                    invItemCanvas.enabled=(true);
                   
                }
            }
        }
    }

    public void SetInvItem(GameObject invObject)
    {
        myObject = invObject;
    }

Thanks you guys for pointing me in the right direction!