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 Mac · Mar 30, 2011 at 01:07 AM · instantiateprefabvector3cs0266

Instantiate object basics (Unity 3.2, C#)

Hi all. Having some problems with Instantiating an object into the game world. Nothing fancy. Ultimately, I would like to click on a GUI Button and that will instantiate a node object that attaches to the mouse pointer to create a simple "select and drop" mechanic.

But need to learn how to walk first before running... I made a prefab of a cylinder called "Node". I have some script attached to my Main Camera to handle the game's GUI. All I want at this point is to click on a button to spawn an instance of the Node prefab into the world. Here's some code:

public class ElementsHUD : MonoBehaviour {

public GameObject Node;

// Display buttons void OnGUI() {

GUILayout.BeginArea (new Rect (Screen.width/2 - 200, Screen.height/2 + 100, 300, 300)); GUILayout.Box (textOutput); GUILayout.EndArea ();

 if (GUI.Button(new Rect(100, 300, 100, 20), new GUIContent("Node 1")))
 {       
     GameObject objNode = Instantiate(Node, transform.position, transform.rotation);
 }

My main concern is how do I get the script to reference the cylinder prefab I have sitting in the Hierarchy? Secondary concern is how to instantiate this cylinder at a specific vector? Sorry this seems like a very basic question but would appreciate any pointers.

Thank you!

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
0

Answer by Jason B · Mar 30, 2011 at 01:14 AM

Since Node is public, looking at the script in the inspector should show a field for "Node". To put a prefab in it, either drag and drop the prefab into it or click the little circle to the right and pick from the list.

Also, you might need to put (GameObject) before Instantiate in your code, or it might throw you an error. Like this:

GameObject objNode = (GameObject)Instantiate(Node, transform.position, transform.rotation);

If you need the object at a specific vector, you can either create the vectors from scratch in your instantiation...

GameObject objNode = (GameObject)Instantiate(Node, new Vector3(0,0,0), transform.rotation);

...or you can create a variable up top and plug in a value for it from the inspector.

public Vector3 placementspot;

Something like that, and then use:

GameObject objNode = (GameObject)Instantiate(Node, placementspot, transform.rotation);
Comment
Add comment · Show 1 · 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 Mac · Mar 30, 2011 at 06:30 PM 0
Share

Thanks Jason, that was very helpful! The syntax is a bit unfamiliar to me in terms of putting "(GameObject)" in front of the Instantiate expression. But yeah, before I was getting this error:

error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

In any case, it's something I can read up on. Now on to the next step of spawning my node cylinders to the mouse pointer! Thanks again.

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 object in world C# 3 Answers

Understanding Unity's coordinate system 3 Answers

Add 0.5f to Vector.Right on next instantiated object ? 1 Answer

Stop Children of Instantiated Parent From Offsetting After Changing Position 0 Answers

Spawning a prefab at another object's location 3 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