Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by burgunfaust · Apr 14, 2011 at 05:06 PM · instantiateprefabdynamicencapsulation

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.

Comment
Add comment · Show 18
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burgunfaust · Apr 17, 2011 at 01:39 PM 0
Share

No one else at all?

avatar image Joshua · Apr 28, 2011 at 01:13 PM 1
Share

Well.. if you cannot get my suggestions (especially method #2) to work, it's definitely you ;)

avatar image Wolfram · Apr 28, 2011 at 01:32 PM 1
Share

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.

avatar image flaviusxvii · Apr 28, 2011 at 02:39 PM 1
Share

@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.

avatar image superpig ♦♦ · Apr 29, 2011 at 11:52 PM 1
Share

(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).

Show more comments

6 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Apr 28, 2011 at 01:12 PM 1
Share

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.

avatar image burgunfaust · Apr 28, 2011 at 09:17 PM 0
Share

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.

avatar image burgunfaust · Apr 28, 2011 at 09:28 PM 0
Share

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.

avatar image Joshua · Apr 29, 2011 at 12:10 AM 0
Share

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?

avatar image chrismisballs · Apr 29, 2011 at 01:42 AM 0
Share

Have u tried just putting each one under a different tag, so that u just call the tag when instantiate the script?

Show more comments
avatar image
2

Answer by efge · Apr 14, 2011 at 06:15 PM

You should use OnMouseDown:

var myName : String;

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burgunfaust · Apr 14, 2011 at 06:20 PM 0
Share

That gave me the same problem.

avatar image burgunfaust · Apr 21, 2011 at 02:51 PM 0
Share

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.

avatar image equalsequals · Apr 27, 2011 at 06:44 PM 0
Share

Can you confirm that the other 2 objects actually have the above script attached, and also have colliders?

avatar image burgunfaust · Apr 27, 2011 at 11:35 PM 0
Share

Yes. They are attached at creation. I think it may have something to do with the prefab/parent relationship.

avatar image Eric5h5 · Apr 27, 2011 at 11:38 PM 2
Share

@burgunfaust: it's working, you just have "collapse" turned on in the console.

Show more comments
avatar image
2

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,

  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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burgunfaust · Apr 27, 2011 at 12:58 PM 0
Share

I have been using method number 1 to do this. The script I posted above was attached to one of the object instances.

avatar image burgunfaust · Apr 27, 2011 at 11:39 PM 0
Share

$$anonymous$$ore specifically to the prefab, so all instances have it.

avatar image burgunfaust · Apr 28, 2011 at 12:06 AM 0
Share

This also did not work. I think that it may be a product of the instantiation.

avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burgunfaust · Apr 25, 2011 at 02:04 PM 0
Share

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.

avatar image burgunfaust · Apr 25, 2011 at 02:04 PM 0
Share

But thanks though.

avatar image Scribe · Apr 25, 2011 at 04:59 PM 0
Share

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

avatar image burgunfaust · Apr 25, 2011 at 05:26 PM 0
Share

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.

avatar image burgunfaust · Apr 29, 2011 at 02:08 AM 0
Share

It's not that they are prefabs, I don't think. I imagine it is because they are instantiated prefabs.

avatar image
0

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); }

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image equalsequals · Apr 27, 2011 at 06:39 PM 0
Share

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.

avatar image AngryOldMan · Apr 27, 2011 at 08:57 PM 0
Share

oops...yes your quite right made edits now thank you

avatar image burgunfaust · Apr 27, 2011 at 11:37 PM 0
Share

I've tried using the "this" variation before and it didn't work, but for completeness, I will try this as well.

avatar image burgunfaust · Apr 27, 2011 at 11:45 PM 0
Share

Exact same results, just one of the instances works.

avatar image burgunfaust · Apr 28, 2011 at 12:07 AM 0
Share

I think that it may be a product of the instantiation.

Show more comments
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges