• 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
0
Question by nongenre · Apr 23, 2012 at 02:06 AM · javascriptprefabinstantiation

Running Script on current prefab only (not all intances)

This is just my first time messing around with instantiation and prefabs, and I've hit a problem I'm not sure how to solve.

The following script lives on a prefab which is used many times in my scene (a chunk of street). The goal is to instantiate another prefab (a crosswalk) on the street when clicked. When I run the game to test this, however, clicking any street spawns a crosswalk on every street chunk in the scene. I only want to instantiate on the chunk clicked. How should I approach this?

 var prefab : GameObject; 

 function Update () {

 // Write some quick and dirty script for placing a crosswalk.
 if(Input.GetMouseButtonDown(2))
 Instantiate(prefab,transform.position, transform.rotation);
 Debug.Log("Player Clicked on a Street Segment");
 }
Comment
Add comment · Show 1
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 nongenre · Apr 27, 2012 at 11:34 PM 0
Share

So I never found a solution to the compile error I complained about below, but the solution offered did help me make sense, and the optimized solution worked perfectly.

2 Replies

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

Answer by syclamoth · Apr 23, 2012 at 02:14 AM

The problem is, you are doing nothing to determine which section of street you want to instantiate a crosswalk on! The code as it stands will execute on every street in the scene as soon as you press the mouse (which, of course, you already know).

The easiest way to manage this would be to shoot a raycast from the position of the mouse, and find out which one it hit. A quick modification to your script will do this.

First, make sure you have a collider on your street segment.

 if(Input.GetMouseButtonDown(2))
     if(collider.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition)))
     {
         Instantiate(prefab,transform.position, transform.rotation);
         Debug.Log("Player Clicked on a Street Segment");
     }
 }

Of course, this will have to calculate as many raycasts as there are roads- which could get a bit inefficient after a while. Instead, you should put the raycast on a single manager script, and then just put a common tag 'CreateCrosswalk' on all the road segments-

 if(Input.GetMouseButtonDown(2))
     var hit : RaycastHit;
     if(Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), hit))
     {
         var hitTrans : Transform = hit.transform;
         if(hitTrans.gameObject.tag == "CreateCrosswalk")
         {
             Instantiate(prefab, hitTrans.position, hitTrans.rotation);
             Debug.Log("Player Clicked on a Street Segment");
         }
     }
 }

Then, you will only do one raycast per click!

Comment
Add comment · Show 2 · 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 nongenre · Apr 23, 2012 at 12:37 PM 0
Share

Ah, makes sense - glad to be doing my first raycast! However - when I try to match your first script example, I get an error I don't quite understand:

 The best overload for the method 'UnityEngine.Collider.Raycast(UnityEngine.Ray, ref UnityEngine.RaycastHit, float)' is not compatible with the argument list '(UnityEngine.Ray)'.

I read up on raycasts and tried this a few different ways - and verified that both the road and crosswalk prefabs have $$anonymous$$esh Collider components (they both started life as a simple Plane object) - but am co$$anonymous$$g up empty so far.

avatar image syclamoth · Apr 26, 2012 at 02:40 AM 0
Share

Sorry I haven't replied earlier, I don't have access to the internet all the time right now.

Try this line ins$$anonymous$$d:

 var hit : RaycastHit;

 if(collider.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), hit, $$anonymous$$athf.infinity))


$$anonymous$$eep in $$anonymous$$d that the entire point of that example is that it's a bad idea- you should just do the second thing, anyway.

avatar image
0

Answer by Vyross · Jan 08, 2014 at 12:00 AM

Instead of using the update method with a check for the mouse button being held down, use the OnMouseDown() method. Make sure your object has a collider.

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

It uses raycasting under the hood, but it prevents code obfuscation.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Prefab Cannot Select Object Correctly 0 Answers

Prevent Additional GameObjects from instatiation 1 Answer

Instantiate prefabs just before it comes into view 2 Answers

Instantiated PreFab selection problem 1 Answer


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