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()
{
}
}
