Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Eowyn27 · Dec 31, 2013 at 08:24 PM · c#instantiateprefabobjectonmousedown

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

Although the picture below is a little glitchy, I want to be able to instantiate those three cylinders after I click on the object onMouseDown. I want to be able to have those three cylinders in the exact position and alignment as on the object. How do I do that?

Also, how would I return to just the single main object (the tree) if I want to. I'm not sure how to implement something like that either.

Am I supposed to create prefabs and instantiate those?

Thanks!

alt text

treesample.png (19.8 kB)
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

3 Replies

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

Answer by HappyMoo · Dec 31, 2013 at 09:03 PM

Build your hierarchy like this:

 Tree
   Cylinders
     Cylinder1
     Cylinder2
     Cylinder3

Then put a collider on the Tree. This will define what you can click. You get most precision if it's a mesh collider, but maybe a capsule collider is enough - you decide.

Cylinders is an empty gameObject(CTRL+SHIFT+N)

Then add the ClickSwitch MonoBehaviour to Tree and drag Cylinders in the obj reference and you're good to go.

 public class ClickSwitch : MonoBehaviour {
     public GameObject obj;
     void OnMouseDown() {
         obj.SetActive(!obj.activeSelf);
     }
 }
Comment
Add comment · Show 13 · 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 Eowyn27 · Dec 31, 2013 at 09:55 PM 0
Share

That makes me think, how do I set only my gameobjects active= false then set them true when the user clicks on it?

Thanks.

avatar image HappyMoo · Dec 31, 2013 at 11:51 PM 0
Share

Can you specify exactly what you want? Click on the tree switches visibility of the cylinders?

avatar image Eowyn27 · Jan 01, 2014 at 12:37 AM 0
Share

yes exactly that.

avatar image Eowyn27 · Jan 02, 2014 at 03:33 AM 0
Share

I'm getting this error: Error CS1061: 'UnityEngine.Transform' does not contain a definition for 'setActive' and no extension method 'setActive' accepting a first argument of type 'UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp)

avatar image JNetRocks1316 · Jan 02, 2014 at 03:34 AM 1
Share

You're getting that error because you don't enable the Transform, you enable the gameObject. change

public Transform obj; to public GameObject obj;

Show more comments
avatar image
0

Answer by DaveA · Dec 31, 2013 at 08:36 PM

I think you would use this:

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

using one of the ones that returns the 'hit' information. That will contain the point at which the object was hit, and the normal at that point too in case you need it.

Prefabs are good. That way if you want to change what you are placing there you just change the prefab.

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
avatar image
0

Answer by J-R-Wood · Jan 26, 2014 at 07:47 PM

I think this is probably already answered, but if not. have you thought of doing like a instiate object on collision, and have that as the place to instiate at. or do a on collision and have that as the game object to setactive

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.

Follow this Question

Answers Answers and Comments

22 People are following this question.

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

Related Questions

How to destroy instantiated objects. 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

Object after instantiate doesn't apply AddForce 1 Answer

Instantiate prefab (button, texture) with attached script 1 Answer

Clones are still in the hierarchy after exiting play mode 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