I have a button that instantiates a prefab game object. When that game object is selected with the mouse, it has a boolean var ‘selected’. Problem is, every instance I instantiate seems to turn the ‘selected’ var on and off regardless of whether it’s the one being clicked on. I have a feeling I’m doing something very basic wrong here. Anyone know? I’d like it to only turn the selected objects bool off/on.
SCRIPT 1
using UnityEngine;
using System.Collections;
public class LevelScriptDragDrop : MonoBehaviour {
public GameObject spawnObject; //this is the prefab to be spawned when button clicked.
public GameObject spawnPoint;
public void SpawnObject(){ //this function is called when button is clicked.
GameObject newPiece = Instantiate(spawnObject, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
}
}
SCRIPT 2
#pragma strict
private var ray:Ray;
private var hit:RaycastHit;
public var selected:boolean = false;
function Update () {
//MOUSE ACTIVITY
if(Input.GetMouseButton(0)){
selected = true;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit) && hit.transform == this.transform)
{
this.transform.position.x = hit.point.x;
this.transform.position.z = hit.point.z;
}
}
if(!Input.GetMouseButton(0)){
selected = false;
}
}