Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Grimfox · May 10, 2016 at 12:41 AM · errorinstantiatetutorialscorespace shooter

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: alt text 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
     }
 }

ssterrormessage.jpg (15.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges