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
12
Question by kumarc123 · Jul 26, 2013 at 10:54 AM · accessing from any scripthow many

how to duplicate a gameobject using scripting?

Hi everyone,

I am making one infinite runner game. In that game I am generating many obstacles, bonuses etc. Can anybody tell me how to duplicate a transform or gameobject. I need the script without Instantiate method. if any method exists for duplicating a gameobject please let me know.

Actually what I am trying to do is instantiating takes some time. If I duplicate an already instantiated gameobject I just need to make a clone of it instead of reinitiating a similar gameobject or maintaining an object pool before loading the scene

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

2 Replies

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

Answer by Bunny83 · Jul 26, 2013 at 11:05 AM

Instantiate IS the duplicate command in Unity. If the object is compilcated (with lots of childs, meshes, ...) it will take some time. there's no way around that.

See those pages: Instantiate Prefabs and Instantiate.

If Instantiate is too slow for you, you should instantiate all at the beginning and just move / enable / disable them as you need them.

Comment
Add comment · Show 3 · 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 kumarc123 · Jul 26, 2013 at 12:26 PM 0
Share

Thanks for your answer. Currently I am keeping an object pool and enabling and disabling gameobjects. I think this is the fastest way.

avatar image Bunny83 · Jul 26, 2013 at 12:39 PM 0
Share

Well, it depends on your platform. In my tests enabling and disabling objects was actually as slow as Instantiate and Destroy on my Nexus7. I guess this was mainly because my objects have $$anonymous$$eshColliders. If you enable or disable $$anonymous$$eshColliders Unity has to update it's physics system which could take a while.

For an endless game you could simply try to keep the sections enabled and just move them ahead when needed. Since passed sections are behind the camera they are frustum culled anyways. Of course they still require some processing, but it makes the "instantiating" faster.

avatar image Aethenosity Bunny83 · May 17 at 11:02 PM 0
Share

If you instantiate an object it also has to update the physics system. So it does the same thing AND creates the gameobject. I'd like to see your tests and the results, because I don't see how it's possible that it would take longer

avatar image
2

Answer by whosdoom · Nov 18, 2020 at 07:33 AM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MakeEnemy : MonoBehaviour {

 public GameObject enemyMake;
 
 void Start () {
     nasru = 3;

 }

 float aralik;

 void Update () {
     nasru -= Time.deltaTime;

     if(nasru <= 0)
     {
         nasru = 3;

         GameObject go = GameObject.Instantiate(enemyMake);
         go.transform.position = transform.position;
     }

 }

}

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 whosdoom · Nov 18, 2020 at 07:37 AM 0
Share

add that code to the plane. The enemy will created at the origin of the plane.

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

18 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

Related Questions

How to identify a different camera? Urgent 1 Answer

Problem accessing C# from Javascript 1 Answer

More elegant use of struct and class 1 Answer

Accessing to a Scriptable object from another script 1 Answer

shot delay in between bullets 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