Search Unity

Space Shooter Tutorial Q&A

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

  1. WolvesBlood

    WolvesBlood

    Joined:
    Mar 11, 2017
    Posts:
    3
    Hey i actually kinda have a question... I've followed the tutos and made it to learning how to make an evasive maneuver AI for the Enemy ship and i cant seem to make the script work can someone please help me out I've been looking high and low for an answer but can't find any.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EvasiveManeuver : MonoBehaviour
    6. {
    7.     public float dodge;
    8.     public float smoothing;
    9.     public float tilt;
    10.     public Vector2 startWait;
    11.     public Vector2 maneuverTime;
    12.     public Vector2 maneuverWait;
    13.     public Boundary boundary;
    14.     private GameController gameController;
    15.  
    16.     private float currentSpeed;
    17.     private float targetManeuver;
    18.    
    19.     private Rigidbody rb;
    20.  
    21.  
    22.     void Start ()
    23.     {
    24.  
    25.         StartCoroutine(Evade());
    26.         rb = GetComponent<Rigidbody>();
    27.         currentSpeed = rb.velocity.z;
    28.  
    29.      
    30.        
    31.     }
    32.    
    33.     IEnumerator Evade()
    34.     {
    35.         yield return new WaitForSeconds(Random.Range(startWait.x, startWait.y));
    36.        
    37.         while (true)
    38.         {
    39.             targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x);
    40.             yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y));
    41.             targetManeuver = 0;
    42.             yield return new WaitForSeconds (Random.Range (maneuverWait.x,maneuverWait.y));
    43.  
    44.         }
    45.     }
    46.    
    47.     void fixedUpdate ()
    48.     {
    49.         float newManeuver = Mathf.MoveTowards (rb.velocity.x, targetManeuver, Time.deltaTime * smoothing);
    50.         rb.velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
    51.         rb.position = new Vector3
    52.             (
    53.                 Mathf.Clamp ( rb.position.x, boundary.xMin, boundary.xMax ),
    54.                 0.0f,
    55.                 Mathf.Clamp ( rb.position.z, boundary.zMin, boundary.zMax )
    56.             );
    57.         rb.rotation = Quaternion.Euler(0, 0, rb.velocity.x * -tilt);
    58.     }
    59. }
     
  2. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    WolvesBlood,

    I see a simple typo in your script. Capitialize the 'F'. - FixedUpdate.

    If you made it this far you already know that, just might not have seen it. I didn't look for any other issues.
     
    WolvesBlood likes this.
  3. ZeRuc

    ZeRuc

    Joined:
    Mar 3, 2017
    Posts:
    25
    I've reached the "Explosions" part of the tutorial so far and I have a question - why you used 3 different scripts for an asteroid when you could put all the needed code in just one script?
     
  4. WolvesBlood

    WolvesBlood

    Joined:
    Mar 11, 2017
    Posts:
    3
    Lol omg yeah i did forget that huh.. wow something so small love it, thanks!
     
  5. WolvesBlood

    WolvesBlood

    Joined:
    Mar 11, 2017
    Posts:
    3
    My guess is because the same scripts you made will go for other models later and you wouldn't want every model acting the same as an astroid
     
  6. crystalwizard321

    crystalwizard321

    Joined:
    Mar 12, 2017
    Posts:
    23
    The Audio tutorial, lesson 1 under finishing the game section, states in the upgrade guide that there are no known issues.

    There are actually several issues

    First of all the audio interface has changed. There is no 3d sound check box, and in fact there are only 3 check boxes - force to mono, normalize, load in background. Not sure if this is because we're using the new WebGL or if it's changed for all platforms. Also, in order to make the audio sound good coming through the computer's single speaker, you have to check both force to mono and normalie.

    you also have to check the new Override for WebGL checkbox and enable that section.

    Second issue: we're told in the game to drag our player weapon sound and drop it on the player. Wrong in so many ways. While there's no problem dropping the sound on something, this is a sound that should be applied to the bolt we made as our shot. And rather than writing additional code to deal with that sound, simply dropping the weapon_player sound on the bolt prefab means every single bolt has that sound and that sound doesn't sit there playing unless we write that extra code.

    Same for the player explosion code. It doesn't go on the player. It goes on the player explosion and doesn't need extra code to be written.

    Who ever wrote this tutorial needs to sit down with it and work through every single step with the new version of Unity, and re do the thing. it's just about worthless as it sits - code has to be written differently, some code doesn't even showup in the script API in the same places as it used to, and so on. A tutorial for beginners should NOT force the beginner to have to dig and hunt and read errata (because of lazy developers) in order to follow the tutorial at all.
     
    jimmack and martin31 like this.
  7. FraMarSaMi

    FraMarSaMi

    Joined:
    May 13, 2014
    Posts:
    86
    But why do you use a Quad for bolt and not use and animate a gameobject with the bolt texture?
     
  8. crystalwizard321

    crystalwizard321

    Joined:
    Mar 12, 2017
    Posts:
    23
    Because you are mapping the bolt texture to the object and it'll display on all the sides the object has. if you use a cube, you'll get a cube shaped arrangement of 5 bolts. if you use a sphere, you'll get a curbed, sphereical shaped bolt. When mapping any texture like this to an object in a 3d scene you want what they call bilboards - a flat object that has only one surface which the texture is mapped to. From the front you can see the texture, from the back it looks like the object isn't even there. The GameObject lable isn't really an object you can see in the scene. it's just a lable for all sorts of things that can be considered Game Objects. That's why you use the quad - because it's a single surface polygon which Unity already knows is an object that should show up in the scene.
     
    lauravc69 and FraMarSaMi like this.
  9. FraMarSaMi

    FraMarSaMi

    Joined:
    May 13, 2014
    Posts:
    86
    Ok thank you :)
     
  10. ZeRuc

    ZeRuc

    Joined:
    Mar 3, 2017
    Posts:
    25
    Now I'm at the "Spawning Waves" part of the tutorial, but isn't the code unnecessarilly complicated? I have accomplished almost same results (no waves of asteroids though, but I think they could be implemented with just a few more variables and if's) by using the code similar to the one seen in "Shooting shots" part:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GameController : MonoBehaviour {
    4.  
    5.     public GameObject hazard;
    6.     public Vector3 spawnValues;
    7.     public float spawnRate; //interval at which hazards will spawn
    8.     private float waitTime = 0f;
    9.  
    10.     void SpawnWaves()
    11.     {
    12.         if (Time.time > waitTime)
    13.         {
    14.             Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    15.             Instantiate(hazard, spawnPosition, Quaternion.identity);
    16.             waitTime = Time.time + spawnRate;
    17.         }
    18.  
    19.     }
    20. }
     
  11. crystalwizard321

    crystalwizard321

    Joined:
    Mar 12, 2017
    Posts:
    23
    It's a do/while loop, and a pretty simple one at that. What else would you use? You could use an if/then/else, but is that necessary?
     
  12. ZeRuc

    ZeRuc

    Joined:
    Mar 3, 2017
    Posts:
    25
    It's more than just that, here's the code from tutorial:
    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.     void Start ()
    14.     {
    15.         StartCoroutine (SpawnWaves ());
    16.     }
    17.  
    18.     IEnumerator SpawnWaves ()
    19.     {
    20.         yield return new WaitForSeconds (startWait);
    21.         while (true)
    22.         {
    23.             for (int i = 0; i < hazardCount; i++)
    24.             {
    25.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    26.                 Quaternion spawnRotation = Quaternion.identity;
    27.                 Instantiate (hazard, spawnPosition, spawnRotation);
    28.                 yield return new WaitForSeconds (spawnWait);
    29.             }
    30.             yield return new WaitForSeconds (waveWait);
    31.         }
    32.     }
    33. }
    As for what I would use, I already posted my code above.
     
  13. Bobonevive96

    Bobonevive96

    Joined:
    Feb 26, 2017
    Posts:
    6
    Hi there I have a strange problem with the Mathf.Clamp function here's my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class boundary{
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10.  
    11. public class mover : MonoBehaviour {
    12.  
    13.     private Rigidbody rb;
    14.     public float speed;
    15.     public boundary bound;
    16.  
    17.     void Start () {
    18.         rb = GetComponent<Rigidbody> ();
    19.     }
    20.  
    21.  
    22.     void FixedUpdate () {
    23.         float moveHorizontal = Input.GetAxis ("Horizontal");
    24.         float moveVertical = Input.GetAxis ("Vertical");
    25.  
    26.         Vector3 move = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    27.  
    28.         rb.AddForce (move * speed);
    29.  
    30.         rb.position = new Vector3 (
    31.             Mathf.Clamp(rb.position.x, bound.xMin, bound.xMax),
    32.             0.0f,
    33.             Mathf.Clamp(rb.position.z, bound.zMin, bound.zMax)
    34.         );
    35.     }
    36.  
    37.  
    38. }
    the problem is that when I reach the limit my player doesn't stop but only slow down a lot. I really don't know how to solve this, please help me.
     
  14. ZeRuc

    ZeRuc

    Joined:
    Mar 3, 2017
    Posts:
    25
    The problem is that you're adding force to your spaceship instead of velocity. Replace the line:
    Code (CSharp):
    1.  rb.AddForce (move * speed);
    with:
    Code (CSharp):
    1. rb.velocity = move * speed;
    and it should work fine.
     
  15. crystalwizard321

    crystalwizard321

    Joined:
    Mar 12, 2017
    Posts:
    23
    In the Ending the game lesson, we're told to use Application.LoadLevel to reload the game if someone restarts. But Unity 5.2 generates a 'that's obsolete, use scene manager' error.

    I looked at the scripting api for scene manager but since I'm real new to Unity, I'm not sure what I'm looking for. I did look at the load scene page, but there wasn't anything about reloading the application - and there's only one scene in this game.

    So, does anyone know the correct syntax/code for using scene manager instead of application loadlevel?
     
  16. Krioldar

    Krioldar

    Joined:
    Mar 19, 2017
    Posts:
    1
    Guys, I need help. I'm stuck at the very end of the lesson "Counting points and displaying the score". I checked all my code and checked it with the finished project. Everything is identical. All settings in Unity are also identical. However, during the collision of an asteroid with a shot, this error appears on the screen. As far as I understand, there is no link to the GameController. I do not know what to do.
     

    Attached Files:

    • WHY.png
      WHY.png
      File size:
      448.5 KB
      Views:
      741
  17. ZeRuc

    ZeRuc

    Joined:
    Mar 3, 2017
    Posts:
    25
    If you have only one scene, then the syntax is:

    Code (CSharp):
    1. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    If you want to load another scene, then you can call it by name:

    Code (CSharp):
    1. SceneManager.LoadScene("Main");
     
    Burgers_and_Frys likes this.
  18. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Hi, if you still have this issue, one thing to check is that the game controller script is not set to inactive.

    If I remember from doing the tutorial, sometimes the author would temporarily disable the game controller script to avoid having to dodge asteroids while working on other parts of the game. If you still have that script inactive when your DestroyByContact script tries to call the scoring method in the game controller script it will probably throw a null reference exception.

    It could also be that the game controller script needs to be assigned to the slot on your DestroyByContact script. The GameObject gameController should be set to public (can't see part of the script this your picture), which opens a slot in the inspector for you to drag the Game Controller over to.
     
  19. nwzebra

    nwzebra

    Joined:
    Dec 4, 2015
    Posts:
    16
    I've noticed a bug in the _Completed-Game (version 1.3). The Done_Asteroid 0x prefabs have the capsule collider on the child GameObject, where the model is located, instead of on the parent, like they pounded into our heads in the tutorial. The problem is that the Done_DestroyByBoundary script only destroys the GameObject that the collider is attached to, so the parent GameObjects remain. Simple fix to move the collider, but I thought I would point it out.
     
  20. desertprawn

    desertprawn

    Joined:
    Mar 22, 2017
    Posts:
    2
    Hi
    There seems to be a few comments regarding the creating shots and trying to get the bolt to move.
    I have been through the video several times and made sure everything is in place but still not able to move the bolt.

    using version 5.5.2f1

    the code is correct and the Bolt is set up correctly in the prefab. However the bolt just does not move either with the player firing or just moving the prefab to the hierarchy window.

    can anyone think of any settings of the scene that I may have missed in early sections that would stop the object from moving?

    I can move the bolt manually by controlling the Z in the transform inpsector window. no errors are reported in the console.

    i have also tried using up or right instead of forward to see if i had the 3D scene setup wrong but there is no movement in any direction


    public class mover : MonoBehaviour {
    public float speed=20;

    void Start ()
    {
    GetComponent<Rigidbody>().velocity = transform.forward * speed;
    }
     
  21. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    desertprawn,

    Try the new way of getting the rigidbody component. I think you have the old way listed.

    public float speed = 20;
    private Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    rb.velocity = transform.forward * speed;
    }
     
  22. desertprawn

    desertprawn

    Joined:
    Mar 22, 2017
    Posts:
    2

    Yes tried that way to but no joy...

    the only way I have go it to work is to change the position not the velocity and have updated the position using the following code:

    public float speed=20;
    private Rigidbody rb;
    private Vector3 newPos;

    void Start ()
    {
    rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
    newPos = new Vector3(0.0f,0.0f,Time.deltaTime*speed);
    rb.MovePosition(transform.position + newPos);
    }

    Looking at the unity scripting information it seems that they dont actually recommend changing the velocity but always use the position instead. I am not sure what the difference is with how unity handles position compared to velocity.

    Also I have got the space craft to move and be controlled by using velocity as per the tutorial which makes it more confusing that I cannot get the bolts to move using velocity.
     
  23. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    The main difference between the two, from what I understand, is using the physics engine to apply forces allows Unity to take care of all the complex math involved with recreating actual physical properties and forces. It can all be done with positions and math, and some programmers prefer doing it that way.

    In your case make sure your Rigidbody is properly set up on the bolt prefab, and that isKinematic is not selected.

    With the bolts I think using position would be perfectly acceptable, however, and this is just my own personal philosophy, when someone does something that works, and I can't get it to work I want to know why. I find I learn more when something doesn't work and I am able to figure it out and fix it, than when I follow a tutorial and everything works right the first time.

    So keep at it and it will work out.
     
  24. munguba

    munguba

    Joined:
    Feb 23, 2017
    Posts:
    1
    I just finished the Creating Shot tutorial and when I hit play mode and drag the bolt to the hierarchy (with speed set to 20) the bolt does not move smoothly like in the video. It kind "jumps"from one position to the next. It keeps happeing even if I raise the speed. No idea what it could be. Does anyone have a clue?
     
  25. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    munguba,

    Check your mover script and make sure you have your rb.velocity statement in Start and not in Update or FixedUpdate.
    May not be your issue, but if you continually add velocity every frame it can do some weird things.
     
  26. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10
    Hey!

    I'm on Creating shots -06- Space Shooter at the very end where you test your bolt, and it moves but not fast. It is in the prefabs set at 20 speed but it moves so much slower than in the video and obviously slower than the ship. I have checked the code and tried other codes people have suggested, and have even checked the slideshow on ownCloud for any info, but I have't found the source of the problem.

    using System.Collections;
    using UnityEngine;

    public class Mover : MonoBehaviour
    {
    private Rigidbody rb;
    public float speed;

    private void Start()
    {
    rb = GetComponent<Rigidbody>();
    rb.velocity = transform.forward;
    }
    }


    This is all of the code I used and even though it moves, it doesn't move like it should. What should I do?
     
  27. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    MrSpaceChicken, you need to multiply your velocity by the speed variable.

    rb.velocity = transform.forward * speed;
     
    MrSpaceChicken likes this.
  28. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10
    Thank you so much!
     
  29. DimlyMad

    DimlyMad

    Joined:
    Mar 26, 2017
    Posts:
    4
    Hi, I've updated the code acording to the new 5.1 guide but on Chapter Moving the Player I get this error:
    error CS0246: The type or namespace name `Monobehaviour' could not be found. Are you missing an assembly reference?
    Can anyome help me?

    This is my code :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10. public class PlayerController : Monobehaviour
    11. {
    12.     private Rigidbody rb;
    13.     void Start ()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     public float speed;
    19.     public float tilt;
    20.     public Boundary boundary;
    21.  
    22.     void FixedUpdate ()
    23.     {
    24.         float moveHorizontal = Input.GetAxis ("Horizontal");
    25.         float moveVertical = Input.GetAxis ("Vertical");
    26.  
    27.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    28.         rb.velocity = movement * speed;
    29.  
    30.         rb.position = new Vector3
    31.             (
    32.                 Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    33.                 0.0f,
    34.                 Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    35.             );
    36.  
    37.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    38.     }
    39. }
     
  30. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Not sure how it happened since Unity kind of fills it all out for you, but I believe it should be MonoBehaviour, with a capital 'B'. You have a lower case 'b'. Try changing that and see if it makes a difference.
     
    DimlyMad likes this.
  31. equinoxboldnewt

    equinoxboldnewt

    Joined:
    Mar 25, 2017
    Posts:
    1
    Can someone please explain this to me? I have a problem in which I have everything done and everything is fine, the scripts and everything, but when I click build, it does it, but when I open the executable application, it says this:
     

    Attached Files:

  32. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    were you moving the .EXE file out of the folder it was built to?

    when it builds, in that folder there should be the EXE file and the DATA folder, they have to be in the same location as the ..EXE, as it depends on that data folder.
     
  33. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10
    I'm on Ending the game -15- Space Shooter and this part of the code isn't in the video or on the ownCloud. I'm getting an error on line 40 but I'm not sure what code to put to fix it. (line 40 is SceneManager.LoadScene (SceneManager.LoadScene); -by the why)

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameController : MonoBehaviour {
    7.  
    8.     public Vector3 spawnValues;
    9.     public GameObject hazard;
    10.     public int hazardCount;
    11.     public float spawnWait;
    12.     public float startWait;
    13.     public float waveWait;
    14.  
    15.     public GUIText scoreText;
    16.     public GUIText restartText;
    17.     public GUIText gameOverText;
    18.  
    19.     private bool gameOver;
    20.     private bool restart;
    21.     private int score;
    22.  
    23.     private void Start()
    24.     {
    25.         gameOver = false;
    26.         restart = false;
    27.         restartText.text = "";
    28.         gameOverText.text = "";
    29.         score = 0;
    30.         UpdateScore ();
    31.         StartCoroutine (SpawnWaves());
    32.     }
    33.  
    34.     private void Update()
    35.     {
    36.         if (restart)
    37.         {
    38.             if (Input.GetKeyDown (KeyCode.R))
    39.             {
    40.                 SceneManager.LoadScene (SceneManager.LoadScene);
    41.             }
    42.         }
    43.     }
    44.  
    45.     IEnumerator SpawnWaves ()
    46.     {
    47.         yield return new WaitForSeconds(startWait);
    48.         while (true)
    49.         {
    50.             for (int i = 0; i < hazardCount; i++)
    51.             {
    52.                 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    53.                 Quaternion spawnRotation = Quaternion.identity;
    54.                 Instantiate(hazard, spawnPosition, spawnRotation);
    55.                 yield return new WaitForSeconds(startWait);
    56.             }
    57.             yield return new WaitForSeconds(waveWait);
    58.  
    59.             if  (gameOver)
    60.             {
    61.                 restartText.text = "Press 'R' for Restart";
    62.                     restart = true;
    63.                 break;
    64.             }
    65.         }
    66.     }
    67.  
    68.     public void AddScore (int newScoreValue)
    69.     {
    70.         score += newScoreValue;
    71.         UpdateScore();
    72.     }
    73.     void UpdateScore()
    74.     {
    75.         scoreText.text = "Score: " + score;
    76.     }
    77.     public void GameOver ()
    78.     {
    79.         gameOverText.text = "Game Over!";
    80.         gameOver = true;
    81.     }
    82. }
     
    Last edited by a moderator: Apr 19, 2017
  34. DimlyMad

    DimlyMad

    Joined:
    Mar 26, 2017
    Posts:
    4
    Thank you! Rookie mistake. You just saved me.
     
  35. CheeseBurg

    CheeseBurg

    Joined:
    Mar 27, 2017
    Posts:
    1
    So first of I want to thank the Unity people for creating free tutorials for us to learn from. But I think it is crazy comes with a 23 page PDF of changes needed for the tutorial to work properly. Can they just update the videos?! My colleagues and I find having to jump back a forth between the PDF and video very frustrating.

    Are the other tutorials like this?
     
  36. Halo_Liberation

    Halo_Liberation

    Joined:
    Mar 6, 2017
    Posts:
    26
    you are wickedly smart. kinda wish i could hire you as a personal trainer for unity coding. by chance also since i have been needing to ask, is there a way to see all the unity codes so i can study them?
     
    Last edited: May 21, 2017
  37. Halo_Liberation

    Halo_Liberation

    Joined:
    Mar 6, 2017
    Posts:
    26
    okay so i did some research and nothing has come up. most people seem to just repeat what i have been saying. so i will leave it here again just in case someone can figure it out but let me explain. so im at the counting points part of the tutorial and when i updated the code it suddenly made my asteroids not want to spawn. music works, shots work, and the ship work. if you can please look over the code and explain to me if anything is wrong. i really want to learn unity but the old videos make it nearly impossible.

    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.     public GUIText scoreText;
    14.     private int score;
    15.  
    16.     void Start ()
    17.     {
    18.         score = 0;
    19.         UpdateScore ();
    20.         StartCoroutine (SpawnWaves ());
    21.     }
    22.  
    23.     IEnumerator SpawnWaves ()
    24.     {
    25.         yield return new WaitForSeconds (startWait);
    26.         while (true)
    27.         {
    28.             for (int i = 0; i < hazardCount; i++)
    29.             {
    30.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    31.                 Quaternion spawnRotation = Quaternion.identity;
    32.                 Instantiate (hazard, spawnPosition, spawnRotation);
    33.                 yield return new WaitForSeconds (spawnWait);
    34.             }
    35.             yield return new WaitForSeconds (waveWait);
    36.         }
    37.     }
    38.  
    39.     public void AddScore (int newScoreValue)
    40.     {
    41.         score += newScoreValue;
    42.         UpdateScore ();
    43.     }
    44.  
    45.     void UpdateScore ()
    46.     {
    47.         scoreText.text = "Score: " + score;
    48.     }
    49. }

    its also telling me that the variable for the score text of "gameController hasn't been set. what does this mean and how do i fix this? any advice is very appreciated.
     
  38. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    The Roll A Ball tutorial is up to date with Unity 5.x, so is the 2d UFO tutorial, which is exactly the same as Roll A Ball but with the necessary changes to make it 2D. I think it greatly helped me having done those two first before doing the Space Shooter.
    I don't know what the logistics of redoing the videos would be, though I'm sure it would take a lot of work. And same as you, I too thank Unity for the tutorials, as I would be completely lost without them.
     
  39. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
  40. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Halo Liberation,

    I don't see anything incorrect on your game controller script that would cause the asteroid waves not to spawn. I would make sure the script is not set to inactive in the inspector, also make sure the HazardCount is not set to zero. If neither is the case I would make liberal use of the Debug.Log feature to track down where things are going awry.

    For instance put Debug.Log("In The While Loop") in the first line after While(true) to make sure the co routine is running.
    Then maybe in the for loop to make sure the program is getting that far.

    As for the gameController score text not set, that may mean you need to drag the Score Text object from the hierarchy into the slot on the gamecontroller inspector. He shows this at the very end of the video.

    And, while I greatly appreciate the compliment, I am not wicked smart. Just a stubborn noob trying to learn this stuff like everyone else.
     
    OboShape likes this.
  41. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10

    Thank you but i actually found a better solution right after i read your reply. If you put 0 in the parenthesis the "press 'r' for restart" will work, while it didn't when i put "Main" in parenthesis. Either way it was a big help thanks!
     
  42. TinoAtMixxusStudio

    TinoAtMixxusStudio

    Joined:
    Oct 10, 2013
    Posts:
    20
    Don't know if this is your problem but noticed this could occur when instantiating multiple prefabs at the same time.. If they fire their first shot at the same time the volume got louder. In one case I solved this by giving a random delay.
    I also seem to remember that a Prefab could be instantiated in background one time and then it should behave as expected the next times.
     
  43. Jarirae

    Jarirae

    Joined:
    Mar 31, 2017
    Posts:
    1
    I'm a newcomer to Unity.
    I was following the 6th tutorial video's instructions, but I running into a problem with inputting the speed. I've typed in everything that the video and the updated guide told me to, but the ability to change speed will not pop up under the script component in the inspector tab.
    Here's the code.
    using UnityEngine;
    using System.Collections;

    public class Mover : MonoBehaviour
    {
    private Rigidbody rb;
    public float speed;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    rb.velocity = transform.forward * speed;
    }
    }
    Is it a problem with the code or is there something else i'm doing wrong?
     
  44. Sushiezi

    Sushiezi

    Joined:
    Mar 30, 2017
    Posts:
    1
    Unity 5 +
    Hello , i got a stuck at here ...
    upload_2017-4-1_4-44-2.png
    rigidbody.velocity = movement * speed;
    is not working on unity 5
    can help me ?
     
  45. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10
    Hey! I'm on the extension 2 hour video on Space Shooter around 1:04:00. I put my 2 bolts into a folder in prefabs, and they both became cyan. I kept trying to change the texture back for only one of the bolts but then the other bolt changes also. What should I do?
     
  46. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10
    You need to set up some extra code not mentioned in the video to use rigidbody. right at the top inside MonoBehavior, put: public Rigidbody rb; . After that, put
    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
    Instead of rigidbody.velocity = movement * speed;, put rb.velocity = movement * speed;

    To see the changes that need to be made for the rest of the videos including the one you are on, you can either turn on annotations in the video or go to https://oc.unity3d.com/index.php/s/rna2inqWBBysn6l?_ga=1.138315568.1600429707.1478841913

    for unity's changes for 5+
     
  47. dougpowell

    dougpowell

    Joined:
    Mar 16, 2017
    Posts:
    6
    I am very new to Unity and C#. I have done the Space Shooter up to the point where the bolts move. Here4 is the code I am using to move the bolt.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Mover : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.     public float speed;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.         rb.velocity = transform.forward * speed;
    14.     }  
    15. }
    16.  
    The bolt appears when I drag it into the Hierarchy. But it just sits there and doesn't move forwards at all.
     
  48. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    jarirae - Code looks good. Would seem to be an issue in the editor. Make sure the Mover script is attached the bolt prefab and if the public field speed doesn't show up, remove the script component and then try reattaching it to the prefab. sometimes it just bugs. If still having issue, post an image of your editor with the bolt prefab inspector showing.

    Mr.SpaceChicken - it did same thing for me. I just played around with it until I got it to work. If your still having the issue let me know and I will open up my tutorial and figure out what I did and post it.

    dougpowell - code looks good. Make sure the script is attached to the bolt prefab and that you have set the speed in the inspector. When you set an uninitialized variable in in C#, the compiler will automatically set it to 0. A common issue is to forget to change the speed value.
     
  49. dougpowell

    dougpowell

    Joined:
    Mar 16, 2017
    Posts:
    6
    Thanks. I'll try futzing around with it and rebuild from scratch if necessary.
     
  50. dougpowell

    dougpowell

    Joined:
    Mar 16, 2017
    Posts:
    6


    Here is a screenshot. I see now that I'm getting an error in the Console. I tried making the Mesh Collider Kinematic but that didn't clear the error. Any help would be greatly appreciated.