- Home /
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.
Well.. if you cannot get my suggestions (especially method #2) to work, it's definitely you ;)
Another suggestions: the problem could be that you accidentally instantiated several objects at the same position (for example, creating one each frame while the mouse button is pressed, or something), so several (but not all) instances print the log message. Either way, Update() won't work, either use On$$anonymous$$ouseDown, or Physict.Raycast.
@burgunfaust saying "the script are already attached to the prefab" does NOT $$anonymous$$EAN you've correctly set everything up.
Bottom line, if this works for me, then YOU are having the problem, not the answers.
(At least, not as far as the actual Unity engine is concerned. Obviously, the editor maintains prefab relationships so that it knows how to select/revert/apply changes).
Answer by Joshua · Apr 28, 2011 at 12:32 PM
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[i],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[i]) {
Debug.Log("your clicking on "+objectName[i]
//put a boolean trigger here that enables a gui window displaying the info.
}
}
}
}
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 here if you like.
Just for clarification, if you use the second method (which I'd advice) you attach NOTHING to your prefab. Just have the script attached to something in your scene, add the relevant data (the cam you're using, the prefab you want to instantiate) and edit inside the script the data you want displayed (names) and you're done. I'd advice also putting the OnGUI function inside this script. Advantage is it'll be called from 1 script ins$$anonymous$$d of from one script per object - which might slow your game down significantly.
I am already doing #2. I have a loader that reads a text file at runtime, and turns that data into an array, from which it instantiates each object and adds data to the object, an then moves onto the next object.
To be even clear about where I'm at: At runtime, I push the pause button. In the hierarchy i cann see everything is where it is supposed to be, hence why this is so damn frustrating.
Ehm, the point is: do you also have the part where every frame it checks the mouse position and mouse input, and if mouse is over an object and input is true it loops through all the prefabs to compare them to the raycasthit.collider.gameobject?
Have u tried just putting each one under a different tag, so that u just call the tag when instantiate the script?
Answer by efge · Apr 14, 2011 at 06:15 PM
You should use OnMouseDown:
var myName : String;
function OnMouseDown () { myName = name; Debug.Log(myName); }
Actually a slightly different problem. This will only fire on one instance of my object(which is good) but it won't fire on the other instances at all even if clicked.
Can you confirm that the other 2 objects actually have the above script attached, and also have colliders?
Yes. They are attached at creation. I think it may have something to do with the prefab/parent relationship.
@burgunfaust: it's working, you just have "collapse" turned on in the console.
Answer by Ashkan_gc · Apr 27, 2011 at 03:12 AM
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,
- 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.
- 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.
I have been using method number 1 to do this. The script I posted above was attached to one of the object instances.
$$anonymous$$ore specifically to the prefab, so all instances have it.
This also did not work. I think that it may be a product of the instantiation.
Answer by Scribe · Apr 25, 2011 at 01:53 PM
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
Again it's only working on one of the prefabs, even though all three are instantiated from the same prefab at runtime. It's not even the first or the last, it's the middle prefab. They are all identical at instantiation, I add variables to them from a txt file at runtime.
this script should only show the info of the object clicked, I may be completely confused but for me it worked fine with prefabs :O
It is, but only for the same object that I was already able to get the info to show up on. It is not working on the other two object at all. I get nothing when I click them.
It's not that they are prefabs, I don't think. I imagine it is because they are instantiated prefabs.
Answer by AngryOldMan · Apr 27, 2011 at 06:07 PM
var myName : String; var me : GameObject;
function Awake () { me = this.gameObject; }
function OnMouseDown() { myName = me.name; Debug.Log(myName); }
This would still produce the same result as the og poster's script as all it is checking is if mouse button 0 is down. You need some way of checking if that instance specifically was clicked.
I've tried using the "this" variation before and it didn't work, but for completeness, I will try this as well.
I think that it may be a product of the instantiation.
Your answer
Follow this Question
Related Questions
Instantiate a prefab vs create one dynamically at runtime? 1 Answer
Changing game object based on variable state? 1 Answer
How do I revert prefab instance properties to their defaults? 0 Answers
How to animate an instantiated object in the timeline/director 2 Answers
Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers