Object deletes when a script is added.
I am trying to add a script to an empty game object but when i add it the object deletes. How can i fix this? Here is my code:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class GM : MonoBehaviour {
public int lives = 3;
public int bricks = 20;
public float resetDelay = 1f;
public Text livesText;
public GameObject gameOver;
public GameObject youWon;
public GameObject bricksPrefab;
public GameObject paddle;
public GameObject deathParticles;
public static GM instance = null;
private GameObject clonePaddle;
// Use this for initialization
void Awake ()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy (gameObject);
Setup();
}
public void Setup()
{
clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
Instantiate(bricksPrefab, transform.position, Quaternion.identity);
}
void CheckGameOver()
{
if (bricks < 1)
{
youWon.SetActive(true);
Time.timeScale = .25f;
Invoke ("Reset", resetDelay);
}
if (lives < 1)
{
gameOver.SetActive(true);
Time.timeScale = .25f;
Invoke ("Reset", resetDelay);
}
}
void Reset()
{
Time.timeScale = 1f;
Application.LoadLevel(Application.loadedLevel);
}
public void LoseLife()
{
lives--;
livesText.text = "Lives: " + lives;
Instantiate(deathParticles, clonePaddle.transform.position, Quaternion.identity);
Destroy(clonePaddle);
Invoke ("SetupPaddle", resetDelay);
CheckGameOver();
}
void SetupPaddle()
{
clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
}
public void DestroyBrick()
{
bricks--;
CheckGameOver();
}
}
Answer by robin-theilade · Sep 05, 2015 at 08:15 PM
I'm guessing the code is from the script you're trying to add in the editor. Try renaming the Reset method to Reset2 just to be sure that Unity does not call that method upon adding the script.
Your answer
Follow this Question
Related Questions
How to remove an object if it is outside the radius of my player ? 0 Answers
Have object fly towards camera 0 Answers
How to define the forward direction of an object made in blender 1 Answer
Can't access a method from another script (Object reference not set to an instance of an object) 1 Answer
Assignement object 1 Answer