- Home /
The question is answered, right answer was accepted
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!
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);
Thanks for replying! Sorry to ask, but do you know how to make the clone spawn at 8 Y? Thanks!
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);
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!
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
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 ;).
Follow this Question
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