- Home /
How to clone with a object.
Hello, I have made a 3d game and I want to clone an object. However, when I find a script it spawns somewhere else, Eg: Player is at object 5 but it clones in object 1. I know this is a duplicate but does anyone know how to clone near the object the script is on periodically? Thanks!
Answer by GanemVisk · Dec 15, 2019 at 05:00 PM
This code spawns a cube close to the object it is on every 5 seconds. If you have difficulty adapting it to your needs tell us.
using System.Collections;
using UnityEngine;
public class PeriodicallySpawn : 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);
}
}
}
Thanks for replying! But do you know how to, ins$$anonymous$$d of the cube spawning could you make a variable where I could drag my game object? Thanks!
Answer by Tripleganger · Dec 16, 2019 at 03:11 AM
I slightly changed @GanemVisk 's answer:
using System.Collections;
using UnityEngine;
public class PeriodicallySpawn : MonoBehaviour
{
Vector3 offset = new Vector3(2, 0, 2);
public GameObject my_gameObject;
// Start is called before the first frame update
void Start()
{
StartCoroutine("Spawner");
}
IEnumerator Spawner()
{
while (true)
{
Instantiate(my_gameObject, transform.position + offset);
yield return new WaitForSeconds(5);
}
}
}
You will need to then assign your game object via the Inspector. Remember to make a prefab.
Answer by zacharif · Dec 17, 2019 at 11:32 PM
OK, thanks for replying! I will just make a new post that describes my issue in depth. Thanks!
Your answer
Follow this Question
Related Questions
How to clone on collision 0 Answers
Cloning a game object. 1 Answer
How to make a follow AI 4 Answers
Mouse Follow Controller 2 Answers
Player not following touch after camera is rotated? 0 Answers