Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Sharckan · May 22, 2017 at 05:09 AM · c#objectcollider2donmousedown

Create an object when another one is clicked C#

I have searched for the solution but I couldn't find it, and before explaining my problem I gotta say, I'm new using C#, with this I'm not trying to say that I need an easy answer or way of solving the problem, but I could have not solved it because I don't have a good level of C#.

One solution that I found and I think it's the correct one is this one: http://answers.unity3d.com/questions/332085/how-do-you-make-an-object-respond-to-a-click-in-c.html

 void Update(){
    if (Input.GetMouseButtonDown(0)){ // if left button pressed...
      Ray ray = camera.ScreenPointToRay(Input.mousePosition);
      RaycastHit hit;
      if (Physics.Raycast(ray, out hit)){
        // the object identified by hit.transform was clicked
        // do whatever you want
      }
    }
  }

(Someone in the comments said it's hit.collider not .hit)

But I don't know how to use it. Where he says "// the object identified by hit.transform was clicked" I don't understand what should I put. I wrote Object.hit.transform but that didn't mean anything. Can you guys give me an example?

And the other thing is how to create another object. I have read about the command Instantiate, but I can't create another object. I don't know if create means literally create, or it means to put into the scene (and that's what I want).

In resume, I want that if you click an object, another one is created, in the exact position as I want it to be.

Thx for your time, if I explained myself wrongly please tell me and I'll try to explain it again.

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

1 Reply

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

Answer by SohailBukhari · May 22, 2017 at 06:50 AM

You are not understanding out hit, the out gives you the info about what it hit, like the distance to the hit, the name of the object it hit, the point where it hit, and so on. It can be very helpful in some cases where you wanna know if a laser hit a friendly target or an enemy, for example in your case you want to check tag using hit info. The hit only detected on the collider not the transform, if you detect hit on the transform or gameObject then null exception will be thrown by the editor.

 if (Physics.Raycast(ray, out hit)){
         // the object identified by hit.transform was clicked
         // do whatever you want
 if(hit.collider.tag == "ClickableObject") // check by tag  if it hits with the collider
       }

Then You can create new object by using instantiate when you detected hit.

   if (Physics.Raycast(ray, out hit)){
             // the object identified by hit.transform was clicked
             // do whatever you want
     if(hit.collider.tag == "ClickableObject") // check by tag  if it hits with the collider
 {
 Instantiate(nameOfInstantiatedObject,Position,Rotation)
 }
           }

you can create new object in the unity events,when hit detected the invoke unity event and in his listener create new object.

You should learn about prefabs Prefab run time instantiation

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 Sharckan · May 22, 2017 at 04:37 PM 0
Share

Thank you!!! ^^

avatar image EdwinChua Sharckan · May 23, 2017 at 01:18 AM 1
Share

$$anonymous$$ark it as answer if it solved your problem :)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I move the camera to another object in the scene on mouse down? 1 Answer

How do I instantiate certain objects to appear in a specific spot? 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Manipulating Instanced Object 2 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