How to find another instantiated prefab clone and tell it what to do?

Hello fellow unity users.
I’m making a simple puzzle. Where I instantiate objects from the same prefab and it only works on the first one, can’t find the other ones I instantiate.
I have been struggling to make it work, searched the internet and docs, but I can’t understand how to solve it. I hope someone can help me and explain what I’m doing wrong.

So I have an object-cube in the scene with a child (named anchor) with this script on the child.
When OnMouseEnter the anchor an object-sphere that I instantiate instantly transforms to Anchor’s position, when OnMouseExit of the anchor it sends the sphere out of the screen. And when OnMouseDown it
transforms to Anchor’s position and changes the sphere’s variable so it stays there for good.
But it only works on the first instance the other ones just spawn and don’t get called.
Even when I try to get and change the sphere’s variable to make sure the first one doesn’t get called again .

Here is the Anchor script:

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

public class SnapExit : MonoBehaviour
{
    public GameObject Anchor;
    GameObject obj;
    private NewTiles Newobj;

     void OnMouseEnter()
    {
        obj = GameObject.FindGameObjectWithTag ("TileNew");
            if (obj.GetComponent <NewTiles> ().NewTileSpawned == 0)
        {
            Newobj = obj.GetComponent <NewTiles> ();
        }
        if (obj != null && Newobj.GetComponent <NewTiles>().NewTileSpawned == 0)
            {
            obj.transform.position = Anchor.transform.position;
            }
        }

     void OnMouseExit()
    {        
        if (obj != null) {
            obj.transform.position = new Vector3 (0, 10, 0);
        }
    }

    void OnMouseDown()
    {
        obj.GetComponent <NewTiles> ().NewTileSpawned = 1;
        obj.transform.position = Anchor.transform.position;
    }
}

Here is the Sphere script:

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

public class NewTiles : MonoBehaviour
{
   public int NewTileSpawned; //New Tile we Spawned
 
    void OnMouseEnter()
    {
    }
 
    void OnMouseExit()
    {
     }
}

Press pause. Are they in the scene? Look for them by name.

Code wise, note that line 13 is only going to find one thing, and it could be different each time. It’s just random if there are more than one thing with that tag.

Line 14 uses the obj to do stuff… why are you then LATER checking it for null in line 18? Line 14 would have blown up and exited with a null reference already if obj was null.

For figuring out logic like this, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

The script works but only on the first instantiated object as I mentioned.

Right. After going through the docs once again I understood that obj = GameObject.FindGameObjectWithTag (“TileNew”);
was directly setting the first object to be obj on permanent basis, unless removed. (actually if removed it would reference the next instantiated object, you’d need to remove that later too and so on)

But to make it work as I wanted without deleting the first and therefor after new instantiated objects I needed to use an array or a list to reference other objects and use
GameObject.FindGameObjectsWithTag

So I changed to an array and it works.

Basically if there are multiple objects that have the same tag and we want to get them all, we need to use the method GameObject.FindGameObjectsWithTag( ) and array.