Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 EdwardLee · Oct 25, 2014 at 04:51 PM · gameobjectdynamicselect

How to select a gameobject by script dynamically

alt text

I would like to select a gameobject and put it in scene (maybe multiple times)(dynamically).

In editor , i can drag it to the scene.

But i have no idea to do it by script(C#) dynamically.

Which function i should use ?

Can anyone solve this problem?

I have learnt Java for 2 yrs, in my understanding, I treat the teapot (in the picture) as a class, and i can create many objects of the teapot. Is this logic right?

unityq.png (38.0 kB)
Comment
Add comment · Show 2
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 MrSoad · Oct 25, 2014 at 05:12 PM 0
Share

You need to make it into a prefab then instantiate this prefab at runtime. Look up "Prefabs" and "Instantiate" in the Unity Docs for detailed information :)

avatar image Landern · Oct 25, 2014 at 05:15 PM 1
Share

@EdwardLee, please watch the Unity Learning videos, it will help so much, as for Prefabs as @$$anonymous$$rSoad mentioned, there's a video for that here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dfsp_spirit · Sep 14, 2016 at 12:25 PM

1) Create a prefab

I would suggest to create an empty folder assets/prefabs/ first. Then add a game object to your scene manually in the editor and prepare it the way you want it (animations, maybe movement script, etc.) Then draw the finished template into the assets/prefabs/ folder.

You can now delete the original object from the scene.

2) Use a script to create instances of the prefab dynamically

Here is an example script, taken from the Unity Manual, Section Instantiating Prefabs:

  // Instantiates a prefab in a grid
 
 public GameObject prefab;
 public float gridX = 5f;
 public float gridY = 5f;
 public float spacing = 2f;
 
 void Start() {
     for (int y = 0; y < gridY; y++) {
         for (int x = 0; x < gridX; x++) {
             Vector3 pos = new Vector3(x, 0, y) * spacing;
             Instantiate(prefab, pos, Quaternion.identity);
         }
     }
 } 

You can drag & drop your prefab on the public prefab variable in the Unity editor.

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

6 People are following this question.

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

Related Questions

Fastest way to instantiate thousands of objects at runtime? 1 Answer

Export Game Scene Objects as obj or fbx 1 Answer

Articulation Bodies Vs Rigidbodies in Unity 2020 1 Answer

Find a variable using a string 1 Answer

Memory optimization question 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