- Home /
Script run twice after loadscene (restart)!
Hi, I'm a new into Unity. I want to make a life pickups that randomly generated in an area. I also want to have 1 life pickup only present at the field, so I use GameObject.Find to detect the prefab in the scene. After the life have been picked up, the next pickup will be 5 seconds later. The first life pickup spawns after 5 seconds the game started.
It all worked perfectly, but then I press my retry button and this where it goes weird. The first 5 seconds spawns 2 life pickup. After picking up the two life pickups, it will spawn 1 life pickups. After that, it will spawn 2 life pickups. My prediction is the script run two times after the scene is reloaded. Can someone help me why is this happening?
This is the C# Script for the life pickups (shield pickups)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShieldSpawn : MonoBehaviour {
public GameObject ShieldPrefab;
public Vector3 center;
public Vector3 size;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (GameObject.Find("Shield(Clone)") != null)
{
// this object is on scene
CancelInvoke("SpawnShield");
}
else
{
// this object is a prefab, not instantiate on any scene
Invoke("SpawnShield", 5);
}
}
public void SpawnShield()
{
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), 0, Random.Range(-size.z / 2, size.z / 2));
Instantiate(ShieldPrefab, pos, Quaternion.identity);
}
This is the Retry Button Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ButtonControl : MonoBehaviour {
public void OnClick()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Will there only ever be one picked up ? then if you pick it up , 5 seconds later it will spawn a new one? Is that what needs to be accomplished. ?
Answer by melsy · Apr 01, 2018 at 05:41 PM
add this to an empty game object in the scene
make sure the Player has a rigidbody and Tag "Player"
using UnityEngine;
public class Spawner : MonoBehaviour
{
[SerializeField]
Vector3 center, size;
[SerializeField]
GameObject prefab;
GameObject spawnedShield;
public void CreateNewShield()
{
Invoke("SpawnShield", 5);
}
void SpawnShield()
{
Vector3 pos = center + new Vector3(Random.Range
(-size.x / 2, size.x / 2), 0, Random.Range(-size.z / 2, size.z / 2));
spawnedShield = Instantiate(prefab, pos, Quaternion.identity);
}
}
add this to the shield prefab
public class Shield : MonoBehaviour
{
// Add this into the shield script
// Put a collider on the shield item and also make it trigger
Spawner spawner;
const string player = "Player";
private void Awake()
{
// Not the best way but easy and since it only
// happens ever now and then doesnt hurt anything
spawner = FindObjectOfType<Spawner>();
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag(player))
spawner.CreateNewShield();
}
}