Question by
joshuakeedy · May 06, 2019 at 05:50 PM ·
gameobjectdestroy objectpublic variabledestroy-clones
(Help) I need this script to destroy all game objects in a scene.
Hello, I am trying to make the following script destroy all the balloon clones in the scene but it is not working. If I use the line Destroy(Balloons) it gives me the error Destroying Assets is not permitted to avoid data loss.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class WipeOut : MonoBehaviour
{
//public GameObject WipeOutCounterInternal;
NumOfWipeOutsCounter WipeOutCounterInternal;
public GameObject WipeOutPrefab;
public GameObject Balloons;
bool allowWipeOut = false;
public NumOfWipeOutsCounter numOfWipeOutsCounter;
GameObject temp;
// Start is called before the first frame update
void Start()
{
numOfWipeOutsCounter = WipeOutCounterInternal.GetComponent<NumOfWipeOutsCounter>();
temp = GameObject.Find("Balloons" + "(Clone)");
}
// Update is called once per frame
void Update()
{
if (numOfWipeOutsCounter.WipeOuts >= 0)
{
allowWipeOut = true;
}
if (allowWipeOut = true && Input.GetKeyDown("space"))
{
Instantiate(WipeOutPrefab);
numOfWipeOutsCounter.WipeOuts--;
Destroy(temp);
if(numOfWipeOutsCounter.WipeOuts == 0)
{
allowWipeOut = false;
numOfWipeOutsCounter.WipeOuts = 0;
}
}
}
}
Many thanks, Josh Keedy.
Comment
Answer by tormentoarmagedoom · May 07, 2019 at 10:09 AM
Hello.
First, this line is not correct:
temp = GameObject.Find("Balloons" + "(Clone)");
you forget the space, it should be with a space betweeen baloons and (Clone)
temp = GameObject.Find("Balloons" + " (Clone)");
Hello, I have made that change but I am still getting the error NullReferenceException: Object reference not set to an instance of an object and nothing is happening in the game.