SpaceShooter Tutorial instanced asteroid not taking GameController game object in unity5
I've been messing around with this tutorial for some time and I've been stuck on the same spot forever. So I feel that the issue is something I'm just glazing over. Everything was working fine up until I tried to count the score. When I added that line it throws the error: The asteroid is not destroyed and the bolt is not destroyed it passes right through and continues on. As a test I set both the GameObjectController and gameController to public and checked to see what they were set to during run time. At run time the both variables are set to "None*." This adds a new wrinkle. If the gameController is set to null it's supposed to throw an error. It does not.
Below is the code for the GameController and DestroyByContact. I know this tutorial has been questioned to death. I've dug through a lot of threads and comments trying to find the answer to my problem, no luck. I've added comments to try to better understand my code. If something is absolutely wrong could you please let me know so I can better my understanding. Thanks!
Destroy by contact:
public class destroyBycontact : MonoBehaviour
{
public GameObject explosion;
public GameObject PlayerExplosion;
public int scoreValue;
public GameController gmCTRLR; //create var of class GameController to connect addscore to GameController script
void start()
{
GameObject gameControllerObject = GameObject.FindGameObjectWithTag("GameController"); //find instance of GameController attach to GCO
if (gameControllerObject != null)
{
gmCTRLR = gameControllerObject.GetComponent<GameController>();
}
if (gmCTRLR == null) //if not connected
{
Debug.Log("cannot find 'GameController'"); //return error to console
}
}
void OnTriggerEnter(Collider other) //on contact preform
{
if (other.tag == "Boundary")
{ return; } //if contact with boundary, ignore
Instantiate(explosion, transform.position, transform.rotation); //spawn the explosion
if (other.tag == "Player") //if contact with player
{
Instantiate(PlayerExplosion, other.transform.position, other.transform.rotation); //spawn the player explosion at player position
}
gmCTRLR.AddScore(scoreValue); //add scoreValue to gamecontroller
Destroy(other.gameObject); //destroy contacting object
Destroy(gameObject); //destroy self
//gmCTRLR.addScore(scoreValue); //add scoreValue to gamecontroller
}
}
Game Controller:
public class GameController : MonoBehaviour {
public GameObject hazard;
public Vector3 spawnValue;
public int hazardCount;
public float startWait;
public float spawnWait;
public float waveWait;
public GUIText scoreText;
private int score;
void Start()
{
score = 0; //on start set score to 0
updateScore(); //refresh score display
StartCoroutine (SpawnWaves()); //run in parallel to game code
}
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait); //give the player time to get ready
while (true) //spawn waves
{
for (int i = 0; i < hazardCount; i++) //spawn a some number of asteroids
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValue.x, spawnValue.x), spawnValue.y, spawnValue.z); //pick a random spawn location within the bounds
Quaternion spawnRotation = Quaternion.identity; //pick a random rotation
Instantiate(hazard, spawnPosition, spawnRotation); //spawn the rock
yield return new WaitForSeconds(spawnWait); //wait a bit to spawn the next rock to prevent accidental collisions of rocks
}
yield return new WaitForSeconds(waveWait); //wait some time to spawn next wave
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue; //add new score to existing score
updateScore(); //refresh score display
}
void updateScore()
{
scoreText.text = "Score: " + score; //change the score display to the new score
}
}
Your answer
Follow this Question
Related Questions
spawn object every 10 score points 1 Answer
My script does not work each time i enter Unity unless i add it to the object again. any solutions? 0 Answers
NullReferenceException: Object reference not set to an instance of an object ? 0 Answers
Hello everyone I have a problem with rounding number of my score 2 Answers
Script component seems to be missing when spawning prefab? 2 Answers