- Home /
extra GameObjects Spawning
The script below Spawns Ghosts at random positions every 10 seconds:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GhostSpawner : MonoBehaviour {
float timer;
public List<GameObject> Ghosts;
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if(timer > 10){
Instantiate(Ghosts[Random.Range(0,4)], transform.position = new Vector3(Random.Range(-20,20), 1, Random.Range(-20,20)), transform.rotation);
timer = 0;
}
}
}
This Script Instantiates a set of 6 static GameObjects (PictureFrames) at runtime:
public bool Start() {
GameObject PictureFrame = Resources.Load("Picture") as GameObject;
GameObject Frame = Instantiate(PictureFrame, transform.position, transform.rotation);
Frame.transform.parent = gameObject.transform;
}
The problem is that each time the GhostSpawner spawns, one of the six PictureFrame produces a clone as well (with the same transform).
It is always the same one, and the clone always instantiates at the same time as the spawner (which is what leads me to believe this is the cause of the problem).
Does anyone have any idea why this is happening and how I could stop it?
I'm confused by "public bool Start(){". where is this picture frame script? is it on the Ghost prefabs? If so that would explain why a picture frame is spawning with a ghost in the same spot because you're telling the picture frame transform to match the transform of the object the script is on. I think
Answer by bunnynsnake · Apr 17, 2018 at 07:01 PM
you should try change this part of the code to public void Start() { GameObject PictureFrame = Resources.Load("Picture") as GameObject;
GameObject Frame = Instantiate(PictureFrame, transform.position, transform.rotation); Frame.transform.parent = gameObject.transform; }
Answer by Bee_Bone · Apr 19, 2018 at 09:41 AM
You'v Done it!
Thanks a lot @bunnynsnake.
I had it as a bool because another script was running a yield that waited for the Start method to return true.
But've I've just added a bool at the bottom instead and it's all working well.
Cheers again!
Your answer
Follow this Question
Related Questions
How to compare the positions of two or more instantiated gameobjects (clones) with different tags? 1 Answer
how do I reference a clones of a instantiated game object and only the clones In a script? 1 Answer
Instantiate creates multiple clones. 2 Answers
Problems with spawning items that the players can pick up - Unet 0 Answers
Spawning multiple objects in same area without intersecting each other 0 Answers