Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Space Shooter Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Mar 26, 2015.

  1. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var missileVelocity : float = 100;
    5. var turn : float = 20;
    6. var homingMissile : Rigidbody;
    7. var fuseDelay : float;
    8. var missileMod : GameObject;
    9. var SmokePrefab : ParticleSystem;
    10. var missileClip : AudioClip;
    11.  
    12. private var target : Transform;
    13.  
    14. function Start () {
    15.  
    16. Fire();
    17.  
    18. }
    19.  
    20. function FixedUpdate ()
    21.  
    22. {
    23.     if(target == null || homingMissile == null)
    24.         return;
    25.  
    26.     homingMissile.velocity = transform.forward * missileVelocity;
    27.  
    28.     var targetRotation = Quaternion.LookRotation(target.position - transform.position);
    29.  
    30.     homingMissile.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, turn));
    31.  
    32. }
    33.  
    34. function Fire ()
    35.  
    36. {
    37.     yield WaitForSeconds (fuseDelay);
    38.     AudioSource.PlayClipAtPoint(missileClip, transform.position);
    39.  
    40.     var distance = Mathf.Infinity;
    41.  
    42.     for (var go : GameObject in GameObject.FindGameObjectsWithTag("Enemy"))
    43.         {
    44.             var diff = (go.transform.position - transform.position).sqrMagnitude;
    45.  
    46.             if(diff < distance)
    47.             {
    48.                 distance = diff;
    49.                 target = go.transform;
    50.             }
    51.  
    52.         }
    53.  
    54. }
    55.  
    56. function OnCollisionEnter (theCollision : Collision)
    57. {
    58.  
    59.     if(theCollision.gameObject.name == "Enemy")
    60.     {
    61.         SmokePrefab.emissionRate = 0.0f;
    62.         Destroy(missileMod.gameObject);
    63.         yield WaitForSeconds(5);
    64.         Destroy(gameObject);
    65.     }
    66.  
    67. }
    68.  
    it looks so easy now when you see it xD
     
  2. jeremybristol1959

    jeremybristol1959

    Joined:
    Nov 11, 2015
    Posts:
    6
    I don't know if this has been answered before, but I'm having an issue with the Space Shooter tutorial for adding a scoreboard. I've followed the Unity 4 tutorial videos (following the Unity 5 annotations when applicable), however in the DestroyByContact script, when I include the line "gameController.AddScore (scoreValue);" (which apparently is supposed to trigger code in the GameController script and return somehow) before the two Destroy game object lines, it fails to return to trigger those lines. If I include it afterward, the destroy lines function properly. In both cases, however, the score remains "0" and I have the errors "NullReferenceException: Object reference not set to an instance of an object" (this triggers for every explosion) and "Default GameObject Tag: Player already registered," which triggers once. For whatever reason, it appears that when DestroyByContact tries to access the script in GameController, it fails. There are no annotations for this, so my coding is the same as the Unity 4 video:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. using System.Collections;
    5.  
    6. public class DestroyByContact : MonoBehaviour
    7.  
    8. {
    9.  
    10. public GameObject explosion;
    11.  
    12. public GameObject playerExplosion;
    13.  
    14. public int scoreValue;
    15.  
    16. private GameController gameController;
    17.  
    18.  
    19.  
    20.  
    21. void start ()
    22.  
    23.  
    24. {
    25.  
    26. GameObject gameControllerObject = GameObject.FindWithTag("GameController");
    27.  
    28. if (gameControllerObject != null)
    29.  
    30.  
    31. {
    32.  
    33. gameController = gameControllerObject.GetComponent <GameController>();
    34.  
    35.  
    36. }
    37.  
    38.  
    39.  
    40. }
    41.  
    42. void OnTriggerEnter(Collider other)
    43.  
    44.  
    45. {
    46.  
    47. if (other.tag == "Boundary")
    48.  
    49.  
    50. {
    51.  
    52. return;
    53.  
    54.  
    55. }
    56.  
    57. Instantiate(explosion, transform.position, transform.rotation);
    58.  
    59. if (other.tag=="Player")
    60.  
    61.  
    62. {
    63.  
    64. Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    65.  
    66. }
    67.  
    68. gameController.AddScore(scoreValue);
    69.  
    70. Destroy(other.gameObject);
    71.  
    72. Destroy(gameObject);
    73.  
    74.  
    75. }
    76.  
    77. }
    78.  
     
    Last edited: Nov 12, 2015
  3. jeremybristol1959

    jeremybristol1959

    Joined:
    Nov 11, 2015
    Posts:
    6
    Here's the GameController script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. using System.Collections;
    5.  
    6. public class GameController : MonoBehaviour {
    7.  
    8. public GameObject hazard;
    9.  
    10. public Vector3 spawnValues;
    11.  
    12. public int hazardCount;
    13.  
    14. public float spawnWait;
    15.  
    16. public float startWait;
    17.  
    18. public float waveWait;
    19.  
    20. public GUIText scoreText;
    21.  
    22. private int score;
    23.  
    24. void Start ()
    25.  
    26.  
    27. {
    28.  
    29. score = 0;
    30.  
    31. UpdateScore();
    32.  
    33. Start Coroutine (SpawnWaves());
    34.  
    35.  
    36.  
    37. }
    38.  
    39. IEnumerator SpawnWaves ()
    40.  
    41.  
    42. {
    43.  
    44. yield return new WaitForSeconds(startWait);
    45.  
    46. while (true)
    47.  
    48.  
    49. {
    50.  
    51. for (int i = 0; i < hazardCount; i++)
    52.  
    53.  
    54. {
    55.  
    56. Vector3 spawnPosition = newVector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    57.  
    58. Quaternion spawnRotation = Quaternion.identity;
    59.  
    60.  
    61. Instantiate(hazard, spawnPosition, spawnRotation);
    62.  
    63. yield return new WaitForSeconds(spawnWait);
    64.  
    65.  
    66.  
    67.  
    68. }
    69.  
    70. yield return new WaitForSeconds(waveWait);
    71.  
    72.  
    73. }
    74.  
    75. }
    76.  
    77.  
    78.  
    79. public void AddScore(int newScoreValue)
    80.  
    81.  
    82. {
    83.  
    84. score += newScoreValue;
    85.  
    86. UpdateScore();
    87.  
    88. }
    89.  
    90. void UpdateScore()
    91.  
    92.  
    93. {
    94.  
    95. scoreText.text = "Score: " + score;
    96.  
    97.  
    98. }
    99.  
    100. }
    101.  
     
    Last edited: Nov 12, 2015
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Hi there @jeremybristol1959,

    Any chance you can edit your posts to use code tags please, would make it alot easier to follow.
    read here : http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Couple of things to check if you could, have you added the tag to your GameController and is it exactly the same as your script ( case sensitive).

    also, in for your GameController script in the scene, have you dragged the GUIText for the score to the slot in the inspector?

    Where you have the console message "Default GameObject Tag: Player already registered,".
    check your tags ensure you havent duplicated the tag for 'Player', as this is one of the standard default tags that are included.

    if you can please paste in the full console error, should be easier to whittle down the issue ;)
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You could also get into quaternion slerp, or one of the other smooth damping or move towards functions, if you want to limit and smooth the rate of turn.
     
  6. jeremybristol1959

    jeremybristol1959

    Joined:
    Nov 11, 2015
    Posts:
    6

    Checked, checked, checked (there's a use of Player in an if statement, but that's it--it comes off as a notification rather than an error).

    The rest of the Null error in the Console display says: DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/SCripts/DestroyByContact.cs:36)
     
  7. jeremybristol1959

    jeremybristol1959

    Joined:
    Nov 11, 2015
    Posts:
    6
    Which means the error refers to:

    gameController.AddScore(scoreValue);
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you checked your tags and layers drop down to see if there are duplicate entries of the Tag "player"? Also check the tags and layers panel. There should be no entries here for player or game controller as both of these should be predefined.
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The Probably means you are not getting a reference to game controller and is probably due to an error on the tag.

    To double check can you post a screenshot of your player game object in the hierarchy, your game controller game object in the hierarchy, and your tags and layers panel?
     
  10. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    Any ideas how to "limit" the accuracy of my missile. I don't want it to always hit the target.
    I think you know what I mean. To make it feel more like homing missile intead of just "find target&destroy" missile. I would like it more "curvy" and random ;)
     
  11. Jimbobbedyjobob

    Jimbobbedyjobob

    Joined:
    Nov 12, 2015
    Posts:
    5
    Hey,

    I am a lazy fellow and cant read the ENTIRE thread to check if someone's had the same problems as me.

    I am a programming virgin, and am working through the "Extending Space Shooter" tutorial.

    So until now I have managed to work out all the little issues, like having to Get stuff and things.

    But now, working on the Destroy By Contact script improvements I am lost in the shadow of the valley of death.

    So here's my DestroybyContact script.

    I added in the player and asteroid game object things earlier, cos i couldn't get at them otherwise.

    Now I worry it's shafted everything.

    Any help?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour
    5. {
    6.     public GameObject explosion;
    7.     public GameObject playerExplosion;
    8.     public GameObject Asteroid;
    9.     public GameObject Player;
    10.     public int scoreValue;
    11.     private GameController gameController;
    12.  
    13.     void Start ()
    14.     {
    15.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    16.         if (gameControllerObject != null)
    17.         {
    18.             gameController = gameControllerObject.GetComponent <GameController> ();
    19.         }
    20.         if (gameController == null)
    21.         {
    22.             Debug.Log ("Cannot find 'GameController' script");
    23.         }
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.         if (other.CompareTag ("Boundary") || other.CompareTag   ("Enemy") )
    29.         {
    30.             return;
    31.         }
    32.  
    33.         if (explosion != null);
    34.         {
    35.             Instantiate (explosion, Asteroid.transform.position, Asteroid.transform.rotation);
    36.         }
    37.  
    38.         if (other.tag == "Player")
    39.         {
    40.             Instantiate (explosion, Player.transform.position, Player.transform.rotation);
    41.             gameController.GameOver ();
    42.         }
    43.         gameController.AddScore (scoreValue);
    44.         Destroy(other.gameObject);
    45.         Destroy (gameObject);
    46.     }
    47. }
    48.  
     
  12. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Ayay, what is it thats not working the way it should? or what errors are you getting?
    if theres errors they should show in the console (Window > Console if its hidden).
     
    Jimbobbedyjobob likes this.
  13. Jimbobbedyjobob

    Jimbobbedyjobob

    Joined:
    Nov 12, 2015
    Posts:
    5
    So it's complaining about me not assigning the Asteroid to the instance of the script on the enemy bolt. Which I hoped I could ignore.

    But I just pulled my finger out and filled all the GameObject Asteroid spaces in each gameobject with DestroyByContact script with Asteroid1...

    Now the puppy seems to like me.

    I thought that it was going to only make Asteroid1 go boomboom if i did that. Also the Tutorial says to leave bits empty...
    arg.

    I was wrong.

    It seems to work now ;)

    Thanks though
     
  14. jeremybristol1959

    jeremybristol1959

    Joined:
    Nov 11, 2015
    Posts:
    6
    Capture.PNG Capture1.PNG Capture2.PNG Capture3.PNG
     
  15. jeremybristol1959

    jeremybristol1959

    Joined:
    Nov 11, 2015
    Posts:
    6
    Is there any possibility that some errors are being caused by Visual Studio? After the first lesson or two, Visual Studio took over as the C# program and I haven't figured out how to return to the Unity C# program. I think I might restart from scratch and see if that doesn't help.
     
  16. Jimbobbedyjobob

    Jimbobbedyjobob

    Joined:
    Nov 12, 2015
    Posts:
    5
    I just didnt download/removed Visual Studio and am using Monodevelop.
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Basically, this line:
    Code (CSharp):
    1. var targetRotation = Quaternion.LookRotation(target.position - transform.position);
    ... Says "rotate to face the target".

    You could probably do this with Transform.LookAt()...

    To limit the turn, make this your target point. Make this the value missile is trying to get to. Then using something like Quaternion slerp to reach a value between the current value and the target value. I would also look at the transform class to see if there are any functions that can help you, or perhaps converting the current rotation quaternion and the current target quaternion into vector3's and then use something from the vector3 class, like MoveTowards (http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html) and then setting the rotation back from Euler angles.
     
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    So, I'm confused. What's not working? You should be able to run the project with the scripts as written in the lesson. If things aren't working, yes we could find a work-around, but it would be better if we found the error or issue and learn to build the project properly.
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You can set the script editor in the preferences panel. You can choose monodevelop, sublime text, visual studio, or whatever.
     
    jeremybristol1959 likes this.
  20. Yeoli_Livunts

    Yeoli_Livunts

    Joined:
    Jul 20, 2015
    Posts:
    7
    Ok. I need Help. I have been working on this issue for 3 days. Please help!!!

    Quick backstory: I added 3 more players for a total of 4 players at a time playing the game. That part is A Ok! Works perfectly.

    **My issue is, I can not get the game to end after all 4 player ships have been destroyed.**

    I have variables ( private bool p1Destroyed; etc, etc) that tell the game when the players are destroyed

    I even added a function ( AllPlayersDestroyed(); ) and another variable ( private bool allPlayersDestroyed; ) both to try to call upon the GameOver function from the GameControllers Script.

    Both with the function I added, and without it failed to end the game, enemies continue to spawn forever. :(

    Here is a copy of my script. Please tell me I missed a semi colon or something! (I have no errors so I know this is not true. I hope) Thank you for your time.

    P.S. I am new to scripting, I have about 6 months experience in coding overall and 4 months using Unity. So please help, or point me in the direction of help. I really wanna learn this stuff. Thank you guys and gals so much for your time!



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour
    5. {
    6.     public GameObject explosion;
    7.     public GameObject playerExplosion;
    8.     public int scoreValue;
    9.  
    10.     private GameController gameController;
    11.     private bool p1Destroyed;
    12.     private bool p2Destroyed;
    13.     private bool p3Destroyed;
    14.     private bool p4Destroyed;
    15.     private bool allPlayersDestroyed;
    16.  
    17.     void Start ()
    18.     {
    19.         p1Destroyed = false;
    20.         p2Destroyed = false;
    21.         p3Destroyed = false;
    22.         p4Destroyed = false;
    23.         allPlayersDestroyed = false;
    24.  
    25.         GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
    26.         if (gameControllerObject != null)
    27.         {
    28.             gameController = gameControllerObject.GetComponent <GameController>();
    29.         }
    30.         if (gameController == null)
    31.         {
    32.             Debug.Log ("Cannot find 'GameController' script");
    33.         }
    34.     }
    35.  
    36.     void OnTriggerEnter (Collider other)
    37.     {
    38.         if (other.tag == "Boundary" || other.tag == "Enemy")
    39.         {
    40.             return;
    41.         }
    42.  
    43.         if (explosion != null)
    44.         {
    45.             Instantiate(explosion, transform.position, transform.rotation);
    46.         }
    47.  
    48.         if (other.tag == "Player1")
    49.         {
    50.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    51.             p1Destroyed = true;
    52.         }
    53.  
    54.         if (other.tag == "Player2")
    55.         {
    56.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    57.             p2Destroyed = true;
    58.         }
    59.  
    60.         if (other.tag == "Player3")
    61.         {
    62.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    63.             p3Destroyed = true;
    64.         }
    65.  
    66.         if (other.tag == "Player4")
    67.         {
    68.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    69.             p4Destroyed = true;
    70.         }
    71.  
    72.         if (p1Destroyed && p2Destroyed && p3Destroyed && p4Destroyed)
    73.         {
    74.             AllPlayersDestroyed();
    75.         }
    76.  
    77.         if (allPlayersDestroyed)
    78.         {
    79.             gameController.GameOver();
    80.         }
    81.  
    82.         gameController.AddScore(scoreValue);
    83.         Destroy (other.gameObject);
    84.         Destroy (gameObject);
    85.     }
    86.  
    87.     public void AllPlayersDestroyed ()
    88.     {
    89.         allPlayersDestroyed = true;
    90.     }
    91.  
    92. }
     
    Last edited: Nov 12, 2015
  21. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    that's an easy one :p

    it's on trigger event, so that's why you gameover wont trigger
    you can do something like this
    Code (csharp):
    1.  
    2. if (other.tag == "Player1")
    3.         {
    4.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    5.             p1Destroyed = true;
    6.                  if (p1Destroyed && p2Destroyed && p3Destroyed && p4Destroyed)
    7.                    {
    8.                       gameController.GameOver();
    9.                     }
    10.         }
    in each player trigger

    you can also place that
    Code (csharp):
    1.  
    2. if (p1Destroyed && p2Destroyed && p3Destroyed && p4Destroyed)
    3.         {
    4.             AllPlayersDestroyed();
    5.         }
    and that
    Code (csharp):
    1.  
    2. if (allPlayersDestroyed)
    3.         {
    4.             gameController.GameOver();
    5.         }
    into update function
    so it check's this each frame

    btw. I'd rather keep those "bool p1-p4Destroyed" in game controller, but that's just me :p
     
    Last edited: Nov 13, 2015
    Yeoli_Livunts likes this.
  22. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    That's some next level scripting :/ I think I'll pass
     
  23. Yeoli_Livunts

    Yeoli_Livunts

    Joined:
    Jul 20, 2015
    Posts:
    7

    Thank you so much!!I am about to do all of this right now! You are wonderful! Ha! Easy for you! I searched the web for days for clues and answers and tips, but anyway thank you so much!


    Update:

    OK I did everything but it still isn't working for me, but I feel like I am very close to having it work properly, but right now I'm tired from work today and I will come back to it later after resting. Thank you.
     
    Last edited: Nov 13, 2015
  24. Vlekke

    Vlekke

    Joined:
    Sep 20, 2015
    Posts:
    3
    Hi there, I've been following this project some time now and I'm almost finished. But I've got a problem with the spawning of the enemy ships... Note: I've done some things a little different, i.e. I used my own space ship models that led me to using different scales and I use bigger screen settings...

    The question: how can I set a different spawning rate for the enemy ships in comparison to the meteorites. Thus I want a different spawnWait for those different hazards. Can you please help me out?

    Cheers!
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    For all that if (this) else if (that) else if (the other thing) code, you may want to look into a switch:
    (and darned if I thought we had done one in our scripting section - will remedy)
    Essentially, you will be able to take a series of "cases" and make a "switch" and feed that switch with the current case (eg: PlayerBool) and have the outcome you desire without a ton of "if"s.
     
    Yeoli_Livunts likes this.
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Heh - yes. I know the feeling.

    I was being deliberately vague for a couple of reasons -

    One was that I didn't/don't really have the time right now to develop it and write it out in code.

    Another was by leaving a trail of breadcrumbs and hints, you could discover your own solution.

    If you do feel you want to take a stab at it, I'll help you through it. Just ping me on a post and I'll look at what you've done.
     
  27. wakuwalt7

    wakuwalt7

    Joined:
    Oct 19, 2015
    Posts:
    6
    Sure:

    Code (CSharp):
    1.  
    2. //DestroyByContact script
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class DestroyByContact : MonoBehaviour {
    7.  
    8.     public GameObject explosion;
    9.     public GameObject playerExplosion;
    10.     public int scoreValue;
    11.     private GameController gameController;
    12.  
    13.     void Start()
    14.     {
    15.         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
    16.         if (gameControllerObject != null)
    17.         {
    18.             gameController = gameControllerObject.GetComponent<GameController>();
    19.         }
    20.         if (gameController == null)
    21.         {
    22.             Debug.Log("Cannot find 'GameController' script");
    23.         }
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.         if (other.CompareTag("Boundary") || other.CompareTag("Enemy"))
    29.         {
    30.             return;
    31.         }
    32.  
    33.         if (explosion != null)
    34.         {
    35.             Instantiate(explosion, transform.position, transform.rotation);
    36.         }
    37.  
    38.         if (other.tag == "Player")
    39.         {
    40.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    41.             gameController.GameOver ();
    42.         }
    43.         gameController.AddScore(scoreValue);
    44.         Destroy(other.gameObject);
    45.         Destroy(gameObject);
    46.     }
    47. }
    48.  
    I think this is the right script, right?

    Yeah, i know, it's weird, but it appeared in that way. I suppose the reason must be that the prefabs are saved in that position... i'll fix that soon...
     
  28. Yeoli_Livunts

    Yeoli_Livunts

    Joined:
    Jul 20, 2015
    Posts:
    7

    OK perfect thank you. I am currently figuring this thing out. I will let you know how I do. Thank you so much for your help!:)
     
  29. Vlekke

    Vlekke

    Joined:
    Sep 20, 2015
    Posts:
    3
    How can I set a different spawning rates for the enemy ships in comparison to the meteorites. Thus I want a different spawnWait for those different hazards. Can you please help me out?
    I know that I've asked this before, but I've got the feeling that the other answers are masking my question... xD
     
  30. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    second game controller or just add another
    IEnumerator SpawnWaves ()
     
  31. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    Try making those bools public instead of private. I had a lot of my problems solved when I did that :p (I've got a lot of connections between scripts and this always gets me xD)
     
    Yeoli_Livunts likes this.
  32. MarcopoloR

    MarcopoloR

    Joined:
    Feb 4, 2015
    Posts:
    114
    Well, I made it though most of the tutorial then I ran into a problem I am not sure how to fix. Yes, I am still a programming noob, but I did look through my code to check to see if I could find any obvious mistakes.
    Nothing is getting destroyed by being shot at(but does as it leaves the boundary). I shoot at the asteroids and the explosion goes off, but they are not destroyed. When an asteroid hits the ship the explosions go off but nothing is destroyed.
    I have isolated the problem comes at the end of my "DestroyByContact" code:

    gameController.AddScore(scoreValue);


    Destroy (other.gameObject);


    Destroy (gameObject);


    On the console the first line is:

    Cannot find 'GameController' Script
    UnityEngine.Debug:Log(Object)


    That presumably happens at start because it is only displayed once, then everytime there is a collision with an asteroid and the shot or the player I get this error:

    NullReferenceException: Object reference not set to an instance of an object
    DestroyBycontact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroybyContact.cs:37)


    I sort of get it, it is not reading the instance of the script or something like that, but I cannot figure out why because I have gone over my code multiple times and cannot find a difference between what I did and what the tutorial code did(except where there are difference between Unity 4 and Unity 5).

    Now when I move the first line of the offending code to the bottom, like this:



    Destroy (other.gameObject);


    Destroy (gameObject);


    gameController.AddScore(scoreValue);

    When I move this things blow up the way they are supposed to, but there is no score recorded, and I get the same lines in the log, with the exception that instead of cs.37 at the end it is cs.40

    NullReferenceException: Object reference not set to an instance of an object
    DestroyBycontact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroybyContact.cs:40)


    It is probably some stupid noob thing I am doing, but I am frustrated because I cannot find the problem in my code, and I am not an expert on deciphering these error messages yet. Is it at least obvious to other people what the problem is here?


    One more thing I forgot to add, don't know if this has anything to do with my problem, but on the upgrade guide for some reason page 22 will not load, just hangs there trying to load, so I am not able to see if there are any update issues related to adding the score.

     
    Last edited: Nov 13, 2015
  33. dasneves

    dasneves

    Joined:
    Nov 13, 2015
    Posts:
    2
    I'm having this problem with my code:
    Assets/Scripts/GameController.cs(38,38): error CS1525: Unexpected symbol `Application'

    using UnityEngine;
    using System.Collections;

    public class GameController : MonoBehaviour
    {
    public GameObject hazard;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    public GUIText scoreText;
    public GUIText gameOverText;
    public GUIText restartText;

    private bool gameOver;
    private bool restart;
    private int score;

    // Use this for initialization
    void Start ()
    {
    gameOver = false;
    restart = false;
    gameOverText.text = "";
    restartText.text = "";
    score = 0;
    UpdateScore();
    StartCoroutine(SpawWaves());
    }

    void Update()
    {
    if(restart)
    {
    if(Input.GetKeyDown(KeyCode.R)
    Application.LoadLevel(Application.loadedLevel);
    }
    }

    IEnumerator SpawWaves()
    {
    yield return new WaitForSeconds(startWait);

    while(!gameOver)
    {
    for(int i = 0; i < hazardCount; i++)
    {
    Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    Quaternion spawnRotation = Quaternion.identity;
    Instantiate(hazard, spawnPosition, spawnRotation);
    yield return new WaitForSeconds(spawnWait);
    }

    yield return new WaitForSeconds(waveWait);

    if(gameOver)
    {
    restartText.text = "Press 'R' to restart!";
    restart = true;
    break;
    }
    }
    }

    public void AddScore(int newScoreValue)
    {
    score += newScoreValue;
    UpdateScore();
    }

    void UpdateScore()
    {
    scoreText.text = "Score: " + score;
    }

    public void GameOver()
    {
    gameOverText.text = "Game Over";
    gameOver = true;
    }
    }

    How can i fix it?
     
  34. MarcopoloR

    MarcopoloR

    Joined:
    Feb 4, 2015
    Posts:
    114
    Okay, I feel like an idiot. I found the error, basically I didn't have the GameController object tagged as GameController, so when it executed the FindWithTag("gameController") it found nothing. duh. At least I hope this helps someone with the same problem.
     
    OboShape likes this.
  35. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,
    double check your line
    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.R)
    your missing a closing bracket for the if statement
    try this
    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.R))

    (also, just as a tip if your posting code, can you use code tags, just makes it alot more readable cheers ;) , check sig for link)
     
  36. dasneves

    dasneves

    Joined:
    Nov 13, 2015
    Posts:
    2
    Thanks, with a simple error like that i'm feeling like a idiot.
     
  37. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Not at all
    Every mistake and solution is a step further on the learning curve :)
     
  38. MarcopoloR

    MarcopoloR

    Joined:
    Feb 4, 2015
    Posts:
    114
    Well, I have another problem, this one even more confusing. Ending the game, I get destroyed and it says "Game Over" like it should, but the asteroids keep coming and it doesn't say to press 'R' to play again. Essentially, it seems to be ignoring this line of code:

    Code (CSharp):
    1. if(gameOver)
    2.             {
    3.                 restartText.text = "Press 'R' for Restart";
    4.                 restart = true;
    5.                 break;
    6.             }
    Even wierder the fist time it happened I got an error message saying there was no reference to the restartText variable(cannot remember exactly but i think this is what it said. Then I restarted the game and the error message went away, but I still had the same problem of the loop not ending. So now, no error message but same problem.

    the restartText component is in the variable slot on the Game Controller object script. Here is my code if anyone cares to read through it, mostly posting it so I am posting all the info.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameController : MonoBehaviour {
    5.  
    6.     public GameObject hazard;
    7.     public Vector3 spawnValues;
    8.     public int hazardCount;
    9.     public float spawnWait;
    10.     public float startWait;
    11.     public float waveWait;
    12.  
    13.  
    14.  
    15.  
    16.     public GUIText scoreText;
    17.     public GUIText restartText;
    18.     public GUIText gameOverText;
    19.     private bool gameOver;
    20.     private bool restart;
    21.     private int score;
    22.  
    23.  
    24.  
    25.     // Use this for initialization
    26.     void Start () {
    27.  
    28.         gameOver = false;
    29.         restart = false;
    30.         restartText.text = "";
    31.         gameOverText.text = "";
    32.  
    33.         score = 0;
    34.         UpdateScore ();
    35.  
    36.         StartCoroutine(SpawnWaves());
    37.  
    38.     }
    39.  
    40.     void Update()
    41.     {
    42.         if (restart)
    43.         {
    44.             if(Input.GetKeyDown(KeyCode.R))
    45.             {
    46.                 Application.LoadLevel(Application.loadedLevel);
    47.             }
    48.  
    49.         }
    50.     }
    51.  
    52.     IEnumerator SpawnWaves ()
    53.             {
    54.         yield return new WaitForSeconds (startWait);
    55.         while(true)
    56.         {
    57.             for (int i =0;i < hazardCount; i++)
    58.             {
    59.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    60.                 Quaternion spawnRotation = Quaternion.identity;
    61.                 Instantiate (hazard, spawnPosition, spawnRotation);
    62.                 yield return new WaitForSeconds (spawnWait);
    63.             }
    64.             yield return new WaitForSeconds(waveWait);
    65.  
    66.             if(gameOver)
    67.             {
    68.                 restartText.text = "Press 'R' for Restart";
    69.                 restart = true;
    70.                 break;
    71.             }
    72.         }
    73.     }
    74.  
    75.  
    76.     public void AddScore( int newScoreValue)
    77.     {
    78.         score += newScoreValue;
    79.         UpdateScore ();
    80.     }
    81.         // Update is called once per frame
    82.     void UpdateScore () {
    83.  
    84.         scoreText.text = "Score:" + score;
    85.  
    86.     }
    87.  
    88.     public void GameOver()
    89.     {
    90.         gameOverText.text = "Game Over!";
    91.         gameOver = true;
    92.  
    93.     }
    94. }
    95.  
    and
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour {
    5.  
    6.     public GameObject explosion;
    7.     public GameObject playerExplosion;
    8.  
    9.     public int scoreValue;
    10.     private GameController gameController;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    16.         if (gameControllerObject != null)
    17.         {
    18.             gameController = gameControllerObject.GetComponent <GameController>();
    19.         }
    20.         if (gameController == null)
    21.         {
    22.             Debug.Log ("Cannot find 'GameController' Script");
    23.         }
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.         if (other.tag == "Boundary")
    29.         {
    30.             return;
    31.         }
    32.         Instantiate (explosion, transform.position, transform.rotation);
    33.         if (other.tag == "Player") {
    34.             Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    35.             gameController.GameOver ();
    36.         }
    37.  
    38.         gameController.AddScore(scoreValue);
    39.         Destroy (other.gameObject);
    40.         Destroy (gameObject);
    41.  
    42.  
    43.  
    44.  
    45.     }
    46. }
    47.  
    well, I figured this one out too. I left my spawn count at 50, which means it would just take a while for the current wave to end. I set this a while back when we were working with spawn counts and didn't think it would matter if I left it at that. I must be tired.
     
    Last edited: Nov 13, 2015
  39. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Nice one, I was scratching my head a bit, wondering what what wrong, and stepping through it :)
    Never thought about that, good job.
     
  40. MarcopoloR

    MarcopoloR

    Joined:
    Feb 4, 2015
    Posts:
    114
    sorry, just deleted my post before I saw you replied. I didn't want to clutter this board with what I thought was a dumb mistake that had nothing to do with the code I wrote.
     
  41. stanley_angelino

    stanley_angelino

    Joined:
    Nov 14, 2015
    Posts:
    11
    I'm having one problem. I've just reached Space Shooter (Camera and lighting). I wanted to open the edit menu, and I don't see a setting called (Render Settings).
    Sorry, I've just forgotten to tell you, I'm working in Unity version 5.2.2f1 (32-bit)
     
  42. MarcopoloR

    MarcopoloR

    Joined:
    Feb 4, 2015
    Posts:
    114
    If you are talking about the rendering path and color space render settings open the Edit drop down menu, got - "project settings" about 3/4 of the way down, click on that and another sub-dropdown menu appears with more choices - out of those choices select "Player". In the inspector you should see a bunch of settings, under "other settings" is rendering, click on that and it will give you a bunch of options, the first two being Rendering path, which you can choose forward or deferred, Color Space being the next one where you can choose forward or linear, and then a bunch of check boxes for different rendering options.
     
  43. stanley_angelino

    stanley_angelino

    Joined:
    Nov 14, 2015
    Posts:
    11
    I mean for the ambient light..
     
  44. MarcopoloR

    MarcopoloR

    Joined:
    Feb 4, 2015
    Posts:
    114
    Window-drop down menu- lighting- ambient intensity - is that what you mean?
     
  45. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning Stanley,

    If you check that the annotations are on for the video, there is a mention at roughly the 6 minute mark;

    In Unity v5, these settings are now found in the lighting panel: Window > Lighting : Scene Tab. Please remove the Skybox by selecting it and deleting it. Make sure the Skybox is "None".

    then you can change the ambient color etc as described in video.

    hope that helps.
     
  46. stanley_angelino

    stanley_angelino

    Joined:
    Nov 14, 2015
    Posts:
    11
    I'm usually using Microsoft Visual studio to edit scripts, now that I can't sign in, I decided to edit scripts using MonoDevelop, but I have no way to switch the script editor to the Unity MonoDevelop.
     
  47. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Stanley,

    Within Unity if you go to Edit > Preferences.
    Select External Tools.

    theres a dropdown there where you can select the External script editor you want to use.
    from there you can select monoDevelop that comes bundled with Unity.




    you can always switch back by reselecting, just make a note of what version you have ticked currently.
     
  48. GameBlaster

    GameBlaster

    Joined:
    Nov 14, 2015
    Posts:
    11
    Hey I am really like the 3d space shooter learning tutorial I would like to make my own project with information here, but instead of the game scrolling down from the top, how would I go about making the game scroll from the side instead. Like old classic parsec from Atari?
     
  49. Vlekke

    Vlekke

    Joined:
    Sep 20, 2015
    Posts:
    3
    Thanks! I just added a new game controller! It works great!
     
  50. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Hi there,
    Well if you're looking to get enemies coming in from a different direction you have all the tools in this tutorial.

    you will have to take it step by step. Think about the world co-ordinates and how they will change as you want to change the orientation of the gamespace. for instance your mover script currently moves bullets and asteroids along the Z axis. you could reuse this script, and chance the axis to say X and they would move left to right...

    Get a pen and paper, and draw it out to help think how it is going to change. decide what plane you want the game space to be on, then work from that as a base line thinking on each element and how they should behave differently depending on the change of axis.

    you might be better going through the series again, and noting the things that you will need to change based on orientation, like background, screen size, boundary sizes, spawning positions etc
     
    Adam-Buckner and GameBlaster like this.