Instantiated prefab click script running on all instances

So I have a prefab that is instantiated multiple times in the game view at runtime. I was trying to set it up so that when a player clicks on that particular object certain information appears.

Problem is that the script is doing that for all the objects of that prefab, rather than just the one object clicked. It is giving the correct information, each has a different name, but it gives me all of them.

var myName : String;

function Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        myName = name;
        Debug.Log(myName);
    }
}

This script is attached to the prefab object, and therefore spawns as a part of the prefab.

So, what am I doing wrong?

Javascript only please. Thanks in advance!

EDIT: I just want to thank everyone who is trying to help on this again.

Your efforts will not go unnoticed.

Edit: down below I added an alternative (better, easier, move efficient and more expandable way - if other then name you want to add way more data) way, so check that out first!

Whow, fifth answer already to a fairly simple question. What you do is you use a script to instantiate several instances of the prefab. You said the second one is the one where you want the mouse down?

Have a counter that checks how many you've instantiated

var counter : int = 0; // when you instantiate, on the next line put counter ++;

Now in your update function check if the instantiated object is the first/second/third one, if so add the script.

if(counter == 2) { cloneName.AddComponent("theClickScriptTwo");} //or 1 or 3 of course

This will add a script called theClickScript. You will first have to create a new script with that name, of course. It would look something like this, depending on what you want to happen when you click on it of course.

var myName : String;

function OnMouseDown () {
  myName = name;
  Debug.Log(myName);
}

I'm assuimg for this that you instantiate like this:

cloneName = Instantiate(prefabName,Vector3Position, QuaternionRotation);

Hope the problem is solved now. Because this certainly works and is easy to set up. :)


An other way, which I used myself recently, is to use arrays. You will need an array of instantiated gameobjects and an array of for instances the names you want to display. I'll assume you have n number of instantiated Objects.

var cam : Camera;
var prefab : GameObject;
var objects : Array = new Array (); 
var objectNames : Array = new Array ("nameOne", "nameTwo",...,"nameN");
var objectLocations : Array = new Array (Vector3(a,b,c),Vector3(d,e,f),...,Vector3(x,y,z);

function Start () {

    for(i=0;i<objectLocations.length;i++) {

        newObject = Instantiate(prefab,objectLocations*,Quaternion.identity);*
 *objects.Add(newObject);*
 *newObject.name = "object"+i; //your inst. are now named object1,object2,...,objectn*
*}*
*} //boom, you have a sorted array of the instantiated objects, we can now reference them by number! Remember, the first entry will be i=0*
*function Update() {*
 *var hit : RaycastHit;*
 *if(Physics.raycast(cam.ScreenPointToRay(Input.mousePosition),cam.transform.forward,hit) && Input.GetMouseButtonDown(0)){*
 *for(i=0;i<objectLocations.length;i++) {*
 _if(hit.collider.gameObject == objects*) {*_
 _*Debug.Log("your clicking on "+objectName*_
 <em>_*//put a boolean trigger here that enables a gui window displaying the info.*_</em>
 <em>_*}*_</em>
 <em>_*}*_</em>
 <em>_*}*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em><em>*<p>This way of doing it may seem complicated, but you can have as many instantiations of the prefab, with as many names and positions without having to go through any trouble. I used it for instance to instantiate 11 planets from one prefab and when the mouse was over any of them it displayed a guiWindow with name/planet type/size/distance etcetera. Check it out <a href="http://www.kongregate.com/games/Josh_Amsterdam/space-exploration" rel="nofollow">here</a> if you like. </p>*</em></em>

You should use OnMouseDown:

var myName : String;

function OnMouseDown () {
  myName = name;
  Debug.Log(myName);
}

the problem of your own code is that you are checking if the mouse button is down or not (in general) and you don't check if it's also on your object or not. there are two ways to see if the mouse is on your object or not,

  1. use the function OnMouseDown () and write your code in it, when user clicks on your object, the code will be called for the instance clicked under the mouse.
  2. use raycasting to see what object is under the mouse and then if it was one of the instances of the prefab then get a reference to it and do what you want with it.

the objects should have a collider attached for both methods to work. to code the second method you should have a special component or tag on the instances of the prefab to be able to know them. the code that i copy here is converted from a c# code as in untested but generally it should work.

if (Input.GetMouseButtonDown(0))
{
var r : Ray = Camera.mainCamera.ScreenPointToRay(Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
var h : RaycastHit;
if (Physics.Raycast (r,h))
{
//let's say your script name is infoScript
var a : infoScript = h.collider.gameObject.GetComponent(infoScript);
if (a != null) Debug.Log (a.name);
}
}

it's easy. with camera.ScreenpointToRay you get a ray that moves from the mouse position in screen (but in world space) toward z axis and then cast that ray. then we check that if the object collided with the ray has any infoScript attached and if yes we print it's name. i assumed that the name of this script is infoScript.

var wasClicked: boolean = false;

function OnMouseUp() {
    wasClicked = true;
}

function OnMouseExit () {
    wasClicked = false;
}

function OnGUI() {
    if (wasClicked) {
        GUI.Label(Rect(20, 50, 100, 20), "Name: " +name);
    }
}

if your prefabs have colliders on them you can do this

I am guessing this is the question you wanted to put a bounty on so I hope this helps

Scribe

var myName : String;
var me : GameObject;

function Awake ()
{
   me = this.gameObject;
}

function OnMouseDown()
{
        myName = me.name;
        Debug.Log(myName);
}

So this is not a full answer, but I have found a workaround. My click script would not work unless the transform.z was greater than 10. Very strange. So I moved what needed to be moved and now I can move on. If anyone can shed some light on why, that would be great.