Search Unity

Space Shooter Tutorial Q&A

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

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Do you have any scripts at all on the asteroid model?
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, you can't put the "Destroy" code willy nilly anywhere...

    It needs to be after the "if" statement.

    The point is the you could have either:
    Code (CSharp):
    1.         Destroy(other.gameObject);
    2.         Destroy(gameObject);
    or
    Code (CSharp):
    1.         Destroy(gameObject);
    2.         Destroy(other.gameObject);
    ... as Unity marks these object to be destroyed at the end of the frame, rather then destroying the object immediately upon reading the code.
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is described in the FAQ in the original post for this thread.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please learn to use code tags:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I don't understand the question. Can you please try again?
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please learn to use code tags?
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You bolt seem to be checked as "Is kinematic". IIRC, this bold is being moved by a force, and kinematic rigidbodies ignore these forces. Try deslecting isKinematic.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    A null reference expection means that a reference to an object in your game is not set correctly. You need to properly fix this reference before it will work. To help more, we will need the complete error message and your code tagged properly so we can see the line numbers.
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm not following the error. DestroyByContactunder is probably a typo... but this is not related to the boundary.

    Can you check your facts and details and try again?
     
  10. Djennosa65

    Djennosa65

    Joined:
    Jul 3, 2015
    Posts:
    5
    Hello there, I'm experiencing issues with the boundary portion of the tutorial. I've created and attached the script and everything went fine. Until of course I entered play mode and began to fire but the bolt only traveled a little bit before disappearing. I thought maybe I set boundary scale wrong but when I moved the player ship up more to the point where the previous shot disappeared it shot and fires again only a little before disappearing it's like there is something stopping the bolt to travel fully across the game. This is odd because I had no problems with this before I set the boundary the shots kept firing and traveling like they were supposed to in the previous video. Is this a potential glitch when using unity 5? I tired deleting and re doing the boundary again and still same results. Could it be something with the script, I mean it was fairly simple and I even used the done script just to make sure! And still same problem. Usually for some reason when I leave and come back to it I some fix it but this one just seems odd.
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Could it be that you still have "Use Gravity" checked on the Bolt's Rigidbody? If so, it will be falling down and will be destroyed when it falls out of the boundary.
     
  12. Djennosa65

    Djennosa65

    Joined:
    Jul 3, 2015
    Posts:
    5
    Yup that was the issue I can't believe i forgot to turn that off! Thanks for your help
     
  13. Galafreed

    Galafreed

    Joined:
    Jul 2, 2015
    Posts:
    2
    Apologies for the code format, think this is it now. Regarding the full Error message this is direct copy and paste from console. Still Finding me feet sorry about the hassle.



    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:24)






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

    Djennosa65

    Joined:
    Jul 3, 2015
    Posts:
    5
    Hey I'm having issues with the GUI Score Text portion of the video, I created a new game object renamed it Score Text and then in the inspector tab I added the component GUI Text however nothing appeared! I went back and right clicked on the empty game object "Score Text" and then added GUI text through the drop down menu. This created a Canvas and I was now able to see the Score text, I positioned the text to be at the top left of the screen by changing the transform x pos and y pos in the inspector tab. I placed it as it looked in the video. However when finishing up with the video i went to click and drag my GUI Score Text into the Game Controller Script spot under the Inspector tab however it did not work and when I clicked on the little circle next to the score text box it opened the assets folder and there was nothing for me to click. I've checked my script and compared it to that of the done script and everything matches up. I'm assuming it has something to do with the way I've created the GUI Text?
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Null reference errors have nothing to do with the script (most times).

    Null reference errors (usually) mean your script is correct, but the object reference is missing due to a problem with the way you have set up your scene in the project.

    What is line 24 in your script. The error message is saying that this problem is at line 24 with:
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:24)

    If the line numbers line up with the code you've pasted it's difficult to read as line 24 here is:
    GetComponent<Rigidbody>().position = new Vector3...

    I find it odd that only this one "GetComponent" is not finding the Rigidbody if all the others are...

    Are you getting any other errors?

    Are *all* of the "GetComponent" calls returning an error?

    Do you have a Rigidbody attached properly to the GameObject? Is the script attached to the correct GameObject?

    You may want to follow the upgrade advice in the original post and find the Rigidbody only once in start can hold that reference for the rest of the script.
     
  16. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please check the upgrade notes on the original post and see if that helps. If you're still stuck, please post again. Be aware that UI Text and GUIText are different items.
     
  17. Djennosa65

    Djennosa65

    Joined:
    Jul 3, 2015
    Posts:
    5
    Okay I went and re-watched the roll-a-ball video on adding a score text cause I figured that be the best way to try and do in unity 5. So i created the text as we did in that one and added GUI text component to my "score text" and i was able to grab and drag the score text over to the game controller script and it worked. However upon entering play mode I don't know what happened but my when my collisions happened the objects weren't destroyed, the sound effects and vfx worked but the objects weren't destroyed. I hadn't changed anything in the script so I'm not sure what happened I think I'm just going to start over :( because i don't know what went wrong. I deleted the text I created and went back into play mode just to see what would happen and the waves wouldn't spawn, everything else worked, then I added the Text back like I previously did and same issue!
     
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ok, a couple of things.

    You should definitely be doing incremental backups between each episode so that if you need to back up a step, you don't have to start from the beginning. To do this, quit Unity, duplicate your project, and rename it so you know what chapter you've finished (eg: MyProject-Chapter1, MyProject-Chapter2, etc.).

    Next, note what I said above:
    In this assignment, we are using GUIText, not the newer UI Text elements. If you choose to use the UI Text element, like was done in Roll-a-ball, then you need to be careful, as this is not supported in this project. To use GUIText, please see the original post of this thread. You simply need to add the GUIText (not Text) component manually to the empty GameObject.
     
  19. Kaustus

    Kaustus

    Joined:
    Jul 9, 2015
    Posts:
    2
    Hey there.
    have run into this problem before on the roll a ball tutorial and now on this one as well. After inputing the code to control movement and pressing play to start the scene, I noticed that I had no way of knowing what buttons I was supposed to use to control the player character or tell it which way to go. Is there an easy solution to this? Did I mess up the code?
     
  20. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening Kaustus,

    if you are using the stated
    Code (CSharp):
    1.  void FixedUpdate ()
    2.     {
    3.         float moveHorizontal = Input.GetAxis ("Horizontal");
    4.         float moveVertical = Input.GetAxis ("Vertical");
    within the PlayerController script (and the correct cAsE in these lines), then normally the default key mapping for "Horizontal" and "Vertical" you can use the arrow keys or WASD. but if you find that these keys aren't being recognised.

    I would say have another glance through the script and checking case sensitivity and syntax to start off with to ensure that code isn't the issue.

    if the script is ok and you just want to check what keys are meant to do what, you can have a look at the Docs under the Input Manager and see what inputs are bound to what actions.

    Unity Docs for input manager
    http://docs.unity3d.com/Manual/ConventionalGameInput.html

    or if video tutes are preferred, Matt has touched on the Input Manager in this live session in the archive
    http://unity3d.com/learn/tutorials/...ng-archive/local-multiplayer-with-controllers
     
  21. mastrius0713

    mastrius0713

    Joined:
    Jul 6, 2015
    Posts:
    1
    Is there a page somewhere that simply displays all the Space Shooter C# codes as updated for Unity 5?
     
  22. Huut

    Huut

    Joined:
    Jan 28, 2015
    Posts:
    1
    Yeahhhh... Many Thanks.
    Same problem here and this did the trick!
     
  23. PixelatedSkyLord

    PixelatedSkyLord

    Joined:
    Jul 10, 2015
    Posts:
    6
    Hi Adam, I just finished the Roll-a-Ball project with relative ease, so I went on to the next project and have run into a problem before I've even started. I can't download the assets for it. I hit open in Unity but it didn't download the assets, just opened the project. Help me pls!
     
  24. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    have you tried opening the asset store from within Unity just to check?

    under Window > Asset Store.

    just a thought to try.
     
  25. Deleted User

    Deleted User

    Guest

    Hello:

    I have finished the tutorial, woot!

    The only remaining nagging problem is that I am on PC and I can't seem to name the build what I want in the builds folder. In the tutorial (which is done on a Mac, presumably), you are able to name your build. On the PC, you can only name the folder you want the project to appear in, and then the files take on the name of that folder automagically.

    So! If you name the folder Builds, as you named it in the tutorial, then the files inside will also be called ... Builds.html, builds.unity3d, etc.

    Is there a fix for this? In the overall scheme of things, this isn't terrible, but I would like my filenames to actually use the name of the game and not just "builds*"

    Thx bunches for the tutorial and all the help.
     
  26. PixelatedSkyLord

    PixelatedSkyLord

    Joined:
    Jul 10, 2015
    Posts:
    6
    I tried it and it worked, thanks!
     
  27. Hamolot

    Hamolot

    Joined:
    Jul 12, 2015
    Posts:
    2
    Stuck trying to puzzle out an issue on Chapter 10 "Creating hazards" at the "RandomRotator" scripting phase.

    I've followed it through till I need to make the Asteroid tumble, but for some reason it won't move when I move into play mode.

    This is what I've been able to hubble together in an attempt to make it work, and it's the same as the code used by 'LegacyOfWax' with his issue, yet it seemed like it doesn't want to do anything.

    Guessing I've missed something somewhere, so hope it's a simple fix. Unity Issue Picture.png
     
  28. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,
    The first thing that I notice there is your Start function.

    you have
    Code (CSharp):
    1. void start()
    where it should be with a Capital S, it wont run the function,and it wont throw an error either
    Code (CSharp):
    1. void Start()
     
  29. Hamolot

    Hamolot

    Joined:
    Jul 12, 2015
    Posts:
    2
    Thanks very much. That's all that was at fault. I've not had to much time with Code so I wasn't aware the Start needed to be capitalized.

    Thanks again for the help. ^_^
     
  30. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    no probs at all.
    every problem solved is a learning opportunity :)
     
  31. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    On windows, I'd crest two folders: builds, and I builds I'd make the next folder "My Game Title" and then build to that.
     
  32. skyscarer

    skyscarer

    Joined:
    Jul 13, 2015
    Posts:
    1
    I've just finished the Space Shooter tutorial with no issues at all. Just curious to see whether any more tutorial videos were going to get put up on it. Only reason i ask is because i see that the Done Scene when built contains enemy ships and the AI for this but this hasnt been explained in the Space Shooter Tutorial as of yet?
     
  33. jorgex3d

    jorgex3d

    Joined:
    Jul 13, 2015
    Posts:
    1
    OK, i just fixed my error.
     
    Last edited: Jul 13, 2015
  34. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The "done" scene has "stretch goals" for the user to try and achieve. The done scene can be dissected if you want a better understanding on how this was done.
     
  35. bromi

    bromi

    Joined:
    Aug 4, 2014
    Posts:
    4
    Hello.

    I've been trying to make this simple tutorial and even in it I can't achieve smooth, proper 60-fps-feel-like movement.
    It can be seen even in the video -- in


    This forum doesn't allow to specify time on youtube -- its 8:30.

    You can see that the player ship is sort of ghosting or doubling from time to time, and overall feeling is not very good.
    Am I not understanding something simple?
     
  36. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This may be an issue with the recording, or perhaps playback in the editor while the screen capture software is running.

    Regardless of the tutorial, are you seeing any issue with your project deployed to a target build? Eg: a standalone player or mobile device?
     
  37. cyfrostan

    cyfrostan

    Joined:
    Jul 14, 2015
    Posts:
    3
    I've completed the tutorial, which obviously required some modifications of my own as it is a bit outdated, but there's still a lot of things in the done build of the game that are not covered in the tutorial. Are we expected to figure it out on our own or is there supposed to be some follow up tutorial to that?

    I will admit that some of this stuff is relatively easy, for example making different kinds of asteroids is a piece of cake, but I think it would be nice if there was a part to this tutorial that covered the enemy AI and spawns, it was actually the part I was looking forward to the most and it just wasn't in there.
     
  38. bromi

    bromi

    Joined:
    Aug 4, 2014
    Posts:
    4
    Hello, Adam.
    Yes, i understand that on youtube it can be a recording issue. But i see the same effect in the 'game' window, and in the standalone build. The movement just is not ideal -- it can be much more 'fluid' on the 60 fps. Can there be some tearing due to fixedstep value of physics?
     
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You can look at the scripts in the done folder and see how it's implemented. These are meant as stretch goals for the student. If you have any questions about it, I, happy to guide you through it.
     
  40. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What is your build target and what is your frame rate?
     
  41. bromi

    bromi

    Joined:
    Aug 4, 2014
    Posts:
    4
    Actually, if i make non-development .exe build, the movement is smoother. Although it still has occasional hiccups.
    In the web-player build (as the tutorial goes), though, the hiccups are much closer to game-windows in the Unity itself.
    Also, somehow even if i set 'Fantastic' quality, i do have those white dots all over the edges of the ship, flickering. You can see a bunch on the screenshot:
    http://i.imgur.com/0WkFmxV.png
    What are they?
    Thank you for your time.
     
  42. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is aliasing. This is the pixel limit of your device. Each pixel must be a single color. When edges of objects move diagonally across a grid of pixels, this is the effect we see. It's an inevitable effect from the medium we are in. You will find it true in all digital mediums.

    https://en.wikipedia.org/wiki/Aliasing
     
  43. Zouron

    Zouron

    Joined:
    Jul 27, 2015
    Posts:
    6
    Hey guys, been working on this tutorial and came across a problem with player movement or more so with limiting player movement on the screen, This is about 14:00 in the video.

    When i play the game, the player gets snapped to the far left position of the screen. Movement is possible in the Z axis but not in the X axis. So basically i can move up/down, but for left/right the player is stuck to the left the playable area. Here is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary{
    6.     public float xMin, xMax, zMin, zMax;
    7. }
    8.  
    9. public class PlayerController : MonoBehaviour {
    10.  
    11.     public float speed;
    12.     public float tilt;
    13.     public Boundary boundary;
    14.  
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    22.         GetComponent<Rigidbody>().velocity = movement * speed;
    23.  
    24.         GetComponent<Rigidbody>().position = new Vector3(
    25.             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    26.             0.0f,
    27.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    28.         );
    29.  
    30.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    31.     }
    32. }
    Hope someone can help me out here.
     
  44. nikoym

    nikoym

    Joined:
    Feb 2, 2015
    Posts:
    1
    When i enter play mode and i have put the loop for on script my asteroids doesnt go straight but at different angles.What is going on?
     
  45. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning,
    Just a quick question, if you highlight your player in the scene, what have you set your boundary to in the inspector? both the xMin and xMax arent both set to -6 are they? as opposed to -6 for the xMin and 6 xMax as these boundary values are used for restricting player movement. Just a thought :)
     
    Zouron likes this.
  46. Zouron

    Zouron

    Joined:
    Jul 27, 2015
    Posts:
    6
    YUP!! that was it :D
    I kept looking at the code thinking something was wrong there.
    Thanks heaps!!
     
    OboShape likes this.
  47. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon @nikoym,
    Thought i would chip in, just to give a little direction to your question.

    you don't mention at what point you are experiencing the problem. If you could, can you explain a little more as to indicate what part along the tutorial you are getting this (vid number, script name etc.)

    you mention that this happens once you attach a script to the asteroid. what script is it you are attaching that gives this problem?

    any errors showing in console? (just in case)

    couple of things to quickly check also.
    Have you set the Boundary and the asteroid colliders to have the 'Is Trigger' box ticked?
    If they are not triggers then you can see once you hit play and click back into scene view, they will look like they are bunching up at the top of the boundary and once they start colliding with each other they will appear to be bouncing off in different directions.

    Cheers
    ObO
     
    Last edited: Jul 30, 2015
  48. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Could you have a value backwards in your boundary definition? The code clamps you inside the boundary. If you had something backwards, say a misplaced - (minus sign/negative), then you could get clamped to the outside of the boundary. Check the boundary script and values in the editor.
     
  49. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Thanks, Obo!

    Y'already done answered that one.
     
    OboShape likes this.
  50. Zouron

    Zouron

    Joined:
    Jul 27, 2015
    Posts:
    6
    Thanks Adam, Obo cleared that up for me.

    I had another question though, in the tutorial, there is mention of adding enemy ships and destroying them too. But it wasn't covered in the tutorial and the assets are given to us. I know the basic logic behind it, but is it covered somewhere else in another video?