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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

The prefab you want to instantiate is null. 2 Answers

Issues with instantiated prefabs 1 Answer

Issue Instantiating prefab in C# 0 Answers

Changes to prefab instance generated from script aren't remembered 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges