Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Dec 21, 2019 at 03:48 PM by zacharif for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by zacharif · Dec 17, 2019 at 11:40 PM · 3dscriptingbasicsfollowclone

Cloning a game object.

Hello! I got a 3D game and I have I want to clone a object. Gamenvisk has politely gave me this code:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class clonefor4 : MonoBehaviour { Vector3 offset = new Vector3(2, 0, 2); // Start is called before the first frame update void Start() { StartCoroutine("Spawner"); } IEnumerator Spawner() { while (true) { **GameObject clone = GameObject.CreatePrimitive(PrimitiveType.Cube);** clone.transform.position = transform.position + offset; yield return new WaitForSeconds(5); } } }
This code works perfectly, but I want to clone a separate game object other than a cube. With only changing the bolded part, is there any way to go about this?

Thanks!

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

  • Sort: 
avatar image
1
Best Answer

Answer by Fariborzzn · Dec 18, 2019 at 12:01 AM

Hey Sure You Can

 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
 
  public class clonefor4 : MonoBehaviour 
 { 
 Vector3 offset = new Vector3(2, 0, 2);
 void Start() 
 { StartCoroutine("Spawner"); } 
 IEnumerator Spawner() 
 { 
 while (true) 
 {
 GameObject clone = GameObject.CreatePrimitive(PrimitiveType.Cube);
  clone.transform.position = transform.position + offset; 
 yield return new WaitForSeconds(5); }
  }
  }

by changing this part :

 GameObject clone = GameObject.CreatePrimitive(**PrimitiveType.Cube**);
 

to this:

     PrimitiveType.Sphere
     PrimitiveType.Capsule
     PrimitiveType.Cylinder
     Or Other Type

If you dont want to try PrimitiveType Change Your GameObject To Prefab then make a reference for it by :

 public GameObject MyObjectToClone;

and then:

 GameObject clone= Instantiate(MyObjectToClone, transform.position + offset, Quaternion.identity);








Comment
Add comment · Show 8 · 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 zacharif · Dec 19, 2019 at 12:08 AM 0
Share

Thanks for replying! Sorry to ask, but do you know how to make the clone spawn at 8 Y? Thanks!

avatar image Fariborzzn zacharif · Dec 19, 2019 at 01:13 PM 0
Share

no need to sorry friend.. after Instantiate your gameobject

 GameObject clone= Instantiate($$anonymous$$yObjectToClone, transform.position + offset, Quaternion.identity);


you should change the position of game object through Transform.Position:

 clone.transform.position=new vector3(clone.transform.position.x,8f,clone.transform.position.z);
avatar image zacharif · Dec 19, 2019 at 11:34 PM 0
Share

Thanks for replying, you're awesome =)! Sadly it still does not work, it still spawns at .5 X. Any ideas? Thanks a lot though, take +1! Also, I think you meant Vector3 ins$$anonymous$$d of vector3. Thanks again!

avatar image Fariborzzn zacharif · Dec 20, 2019 at 02:36 AM 0
Share

Hey zacharif it still spawns at .5 X. Any ideas?

 clone.transform.position=new vector3(clone.transform.position.x,8f,clone.transform.position.z);

in here we are just setting the y value of spawned object you can set the x and z too if the x of your position is 0.5 its because of this line

 GameObject clone= Instantiate($$anonymous$$yObjectToClone, **transform.position + offset**, Quaternion.identity);


transform.position is the position of the gameobject that clonefor4 is attached to so we are getting it and ading our offset (Vector3 offset = new Vector3(2, 0, 2);) To it..

what value you need for set the x and z?

yes i mean Vector3 sorry ide correct me everytime..Its so bad that auto correct is not here lol :D

avatar image zacharif Fariborzzn · Dec 20, 2019 at 06:49 PM 0
Share

Thanks for your explanation! The Z-axis works perfectly. But as I said before I want the X-axis to be 8. Hopes this clears up the confusion. Is there any scripting that could make it snap to X 8, Thanks! Also, I use Grammarly ;).

Show more comments

Follow this Question

Answers Answers and Comments

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

Related Questions

How to clone on collision 0 Answers

How to clone with a object. 3 Answers

How to stop all StopAllCoroutines(); from another script 1 Answer

Make Clone AIs Stay Apart? 1 Answer

Want to make GameObject follow another GameObject's path Unity3D 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