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!
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!
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 instead of vector3. Thanks again!