Search Unity

Space Shooter Tutorial Q&A

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

  1. Qronaz

    Qronaz

    Joined:
    Jan 8, 2018
    Posts:
    1
    Hello, i have a problem that my asteroid is spinning in wide cercles while moving forward...
     
  2. SpadGame

    SpadGame

    Joined:
    Sep 7, 2017
    Posts:
    7
    I'm sorry if this is a common problem, but I'm getting an error during the Moving the Player tutorial. The error is Assets/Scripts/PlayerController.cs(25,1): error CS1525: Unexpected symbol `void'
    It knows void Start() but not void FixedUpdate() and I'm confused why.

    My code is:

    1. using UnityEngine;
    2. using System.Collections;

    3. [System.Serializable]
    4. public class Boundary
    5. {
    6. public float xMin, xMax, zMin, zMax;
    7. }

    8. public class Mover : MonoBehaviour
    9. {
    10. public float speed;
    11. public float tilt;
    12. public Boundary boundary;

    13. private Rigidbody rb;

    14. void Start()
    15. {
    16. rb = GetComponent<Rigidbody> ();
    17. ReadOnlyCollectionBase.velocity = transform.forward * speed;
    18. }
    19. }

    20. void FixedUpdate()
    21. {
    22. float moveHorizontal = Input.GetAxis ("Horizontal");
    23. float moveVertical = Input.GetAxis ("Vertical");

    24. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    25. rigidbody.velocity = movement * speed;

    26. rigidbody.position = new Vector3
    27. (
    28. Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
    29. 0.0f,
    30. Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    31. );

    32. rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    33. }
    34. }
     
  3. StuSMASH

    StuSMASH

    Joined:
    Jan 8, 2018
    Posts:
    1
    Hi,

    I believe you need to be using GetComponent<RigidBoday> since rigidbody is no longer a recognized function:
    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    GetComponent<Rigidbody>().velocity = movement * speed;

    GetComponent<Rigidbody>().position = new Vector3
    (
    Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    );


    Stu
     
  4. SpadGame

    SpadGame

    Joined:
    Sep 7, 2017
    Posts:
    7
    Thank you for the reply, I tried your code, but I'm still getting the same error.
     
  5. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    You have an extra closing curly brace at line 23

    But I also am not sure about some of the other code.

    At line 10, you have the class name defined as Mover
    public class Mover : MonoBehaviour
    but you say it is for the PlayerController.cs script.

    At several places in your FixedUpdate method you use "rigidbody" when I think you want rb.
    eg
    rigidbody.velocity = movement * speed;
    should be rb.velocity = movement * speed;

    There may be a problem in line 21 of the Start method.

    I don't have a Start method in my PlayerController.cs script, so I'm not sure what the line
    ReadOnlyCollectionBase.velocity = transform.forward * speed;
    is meant to do, but I would guess you might want
    rb.velocity = transform.forward * speed;
    if you want anything at all.

    NOTE Stu's suggestions are also correct, (you can work the problem without using "rb" and a Start method) but don't work because of the extra curly at line 23
     
  6. SpadGame

    SpadGame

    Joined:
    Sep 7, 2017
    Posts:
    7
    Thank you. That fixed the problem, but now I'm getting a different issue. It's still not working, and it's telling me
    "The referenced script on this Behaviour is missing!"
    and
    "The referenced script on this Behaviour (Game Object 'Player') is missing!"
    This seems like an easy issue to fix maybe?
     
  7. SpadGame

    SpadGame

    Joined:
    Sep 7, 2017
    Posts:
    7
    By the way, my current script is:

    1. using UnityEngine;
    2. using System.Collections;

    3. [System.Serializable]
    4. public class Boundary
    5. {
    6. public float xMin, xMax, zMin, zMax;
    7. }

    8. public class Mover : MonoBehaviour
    9. {
    10. public float speed;
    11. public float tilt;
    12. public Boundary boundary;

    13. private Rigidbody rb;

    14. void Start()
    15. {
    16. rb = GetComponent<Rigidbody> ();
    17. }

    18. void FixedUpdate ()
    19. {
    20. float moveHorizontal = Input.GetAxis ("Horizontal");
    21. float moveVertical = Input.GetAxis ("Vertical");

    22. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    23. GetComponent<Rigidbody>().velocity = movement * speed;

    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. }
     
  8. Traveler3326

    Traveler3326

    Joined:
    Jan 10, 2018
    Posts:
    1
    I am currently on part 5 of space shooter.

    The game is working fine, I just have a weird issue with the spacing.

    When I am in Scene view my ship is large and if I move it to the left or right to get thex clamp values, my values end up coming to -3 and 3. However, when I play the game and go into game view, the values -3 and 3 are not wide enough for the ship to move around. So, I changed them to -6 and 6 and now the ship moves to the correct areas in play mode. The same thing happens in the Z direction.

    Why are my Scene view and Game views giving different values for the width of the screen?
     
  9. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    At line 10, you have the class name defined as Mover
    Code (csharp):
    1.  
    2. public class Mover : MonoBehaviour
    3.  
    but you say it is for the PlayerController.cs script.
    it should be:
    Code (csharp):
    1.  
    2. public class PlayerController : MonoBehaviour
    3.  
    if it is in the PlayerController.cs script.

    Confirm you referencing the script PlayerController.cs in your 'Player' Game Object
    Confirm it is the script in your working directory, and not the one in the "_Complete-Game" directory
     
  10. Malkalypse

    Malkalypse

    Joined:
    Jan 13, 2017
    Posts:
    14
    Qronaz: make sure that the
    Make sure that both the Asteroid game object (i.e. the one with the rigidbody, collider and scripts), and the asteroid prop (i.e. the one nested inside, with the mesh filter, renderer and material) BOTH have their transforms set to 0,0,0
     
  11. Jackylembush

    Jackylembush

    Joined:
    Jan 10, 2018
    Posts:
    1
    Hello guys !
    Actually making my first steps into unity, and i have an issue with the boundary part 8. My shots get destroyed right, but not the cloneds one. I check every thing since at least 2 hours and i still didnt find the way, so if u can help me !
    Sorry if i dont have a perfect english, working on it :)

    Ha and, here my controller :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Boundary
    7. {
    8.     public float xMin, xMax, zMin, zMax;
    9. }
    10.  
    11. public class PlayerController : MonoBehaviour
    12. {
    13.     private Rigidbody rb;
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.     public float speed;
    20.     public float tilt;
    21.     public Boundary boundary;
    22.  
    23.     public GameObject shot;
    24.     public Transform shotSpawn;
    25.     public float fireRate;
    26.  
    27.     private float nextFire;
    28.  
    29.     void Update()
    30.     {
    31.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    32.         {
    33.             nextFire = Time.time + fireRate;
    34. //           GameObject clone =
    35.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation); // as GameObject
    36.         }
    37.  
    38.     }
    39.  
    40.     void FixedUpdate ()
    41.     {
    42.         float moveHorizontal = Input.GetAxis("Horizontal");
    43.         float moveVertical = Input.GetAxis("Vertical");
    44.  
    45.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    46.         rb.velocity = movement*speed;
    47.  
    48.         rb.position = new Vector3
    49.         (
    50.             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    51.             0.0f,
    52.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    53.         );
    54.  
    55.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);        
    56.      }
    57. }
    and my boundary :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class DestroyByBoundary : MonoBehaviour
    7. {
    8.     void OnTriggerExit(Collider other)
    9.     {
    10.         Destroy(other.gameObject);
    11.  
    12.     }
    13. }
    14.  
    15.  


    Well, after trying some things again, it's like the boundary didnt recognize the cloned bolt as a gameObject, i have no idea why.

    ha and forgot to said, the boundary work well with the player game object, i tried to reduce the area and went in, and player got destroyed as well :p
     
    Last edited: Jan 12, 2018
  12. p-mcdonald329

    p-mcdonald329

    Joined:
    Feb 27, 2016
    Posts:
    1
    Hello,

    When trying to add the Prefab VFX Explosions to the DestroybyTime Script, the script doesn't appear in the Add Component Script list. What to do?
     
  13. retsofh

    retsofh

    Joined:
    Jan 11, 2018
    Posts:
    7
    Screen Shot 2018-01-16 at 10.48.15 AM.png Upgrade Guide:

    Common Upgrade Issues (please see the upgrade guide for details):

    • Unity 5: When using the mesh collider and adding the simplified mesh, the mesh collider must be "convex" when using Unity 5. Please check "convex" on the mesh collider component.
    • Unity 5: Ambient light is now set differently. Please delete the default skybox from the Lighting panel and make sure it is set to "None".
    • Unity 5: Common components are no longer cached and there are no "helper references" access them.
    Common Misunderstandings:
    • My GUIText is not visible on the screen
      • Check to make sure that the Transform Position x and y values are between 0 and 1.
      • In the Transform Position x value is either of the extremes of 0 or 1, make sure that the alignment is not pushing the text off of the screen.
      • Check that any "Parent" GameObject (like a holder game object) is set to "origin", or (0,0,0) in its Transform Position.
    • My Bolt Object is laggy when fired
      • Make sure you have the Rigidbody component set according to the steps in the video and that you only have one Rigidbody in the GameObject family (eg: You don't have one Rigidbody on the root and one on the VFX Quad).

    For the archived thread please use this link:
    http://forum.unity3d.com/threads/space-shooter-tutorial-q-a-archived.222119[/QUOTE]
     
  14. retsofh

    retsofh

    Joined:
    Jan 11, 2018
    Posts:
    7
    [/QUOTE]
    hi there,
    can someone please tell me why this won't compile???
     
  15. retsofh

    retsofh

    Joined:
    Jan 11, 2018
    Posts:
    7
    hi there,
    can someone please tell me why this won't compile???[/QUOTE]
    please forgive me as I am obviously new here. I know this piece of code looks awkward as it was initially passing rb and getting errors. So I went to the 'done' script and saw the use of 'GetComponent<Rigidbody>()' in place of all 'rb' instances. I have switched back to rb reference in lines 32,. 34, 36, 39. still 4 errors

    /Users/retsofh/Desktop/Space Shooter/Assets/Scripts/PlayerController.cs(21,21): Error CS1061: Type `UnityEngine.Rigidbody' does not contain a definition for `positon' and no extension method `positon' of type `UnityEngine.Rigidbody' could be found. Are you missing an assembly reference? (CS1061) (Assembly-Sharp)

    and """quaternium doesn't exist" either.

    I need to start over again, me thinks
     
  16. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    hi there,
    can someone please tell me why this won't compile???[/QUOTE]

    At lines 33 and 35 you have mispelled the word position.
    You have positon
    Also, your code is a little off,
    you have
    GetComponent()<Rigidbody>.positon.x,
    you want:
    GetComponent<Rigidbody>().position.x


    Finally, update line 38
    to be:
    GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f,
    GetComponent<Rigidbody>().velocity.x * -tilt);
    (Note the parens in GetComponent<Rigidbody>().velocity.x)

    And, now that you don't reference rb, you can delete the Start method.
     
    Last edited: Jan 16, 2018
    retsofh likes this.
  17. retsofh

    retsofh

    Joined:
    Jan 11, 2018
    Posts:
    7
    At lines 33 and 35 you have mispelled the word position.
    You have positon
    Also, your code is a liitle off, you want:
    GetComponent<Rigidbody>().position.x

    And, now that you don't reference rb, you can delete the Start method.[/QUOTE]
    :D
    best to read the error report and wear glasses. thanks for easing my pain.
     
  18. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    I figure it out why some of the bolts manage to slip through the boundary without getting destroyed. I wrote somewhere on this forum about the position of the enemy ship. If it reach too close to the boundary, the bolt will actually spawn on the other side of the boundary before the enemy ship get destroy. I just attach destroy by time script to ensure any bolts that manage to spawn on the other side get destroy eventually. I'm just uncertain if this is the best approach.
     
  19. zeen0

    zeen0

    Joined:
    Nov 27, 2017
    Posts:
    6
    Hello Adam and all,

    I am at the last stages of finishing up the extended space shooter but am having difficulties solving 2 issues:

    1. Enemy Shot does not come all the way down the screen

    2. If Evasive Maneuver script is enabled, the enemy does not move along z-axis

    I have attached relevant screenshots. Please help me out!

    Thanks for the tutorials! you're the man! Screen Shot 2018-01-17 at 3.23.38 PM.png Screen Shot 2018-01-17 at 3.23.59 PM.png Screen Shot 2018-01-17 at 3.24.02 PM.png Screen Shot 2018-01-17 at 3.24.15 PM.png Screen Shot 2018-01-17 at 3.24.21 PM.png Screen Shot 2018-01-17 at 3.23.38 PM.png Screen Shot 2018-01-17 at 3.23.59 PM.png Screen Shot 2018-01-17 at 3.24.02 PM.png Screen Shot 2018-01-17 at 3.24.15 PM.png Screen Shot 2018-01-17 at 3.24.21 PM.png Screen Shot 2018-01-17 at 3.23.38 PM.png Screen Shot 2018-01-17 at 3.23.59 PM.png Screen Shot 2018-01-17 at 3.24.02 PM.png Screen Shot 2018-01-17 at 3.24.15 PM.png Screen Shot 2018-01-17 at 3.24.21 PM.png
     
  20. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Can you post your evasion and enemy bolt script as well?
     
    zeen0 likes this.
  21. zeen0

    zeen0

    Joined:
    Nov 27, 2017
    Posts:
    6
    Sure, attached the scripts now
    Thank you
     

    Attached Files:

  22. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Mm..how far does the enemy's shot goes down the screen? And did you purposely disable Mover Script?
     
  23. zeen0

    zeen0

    Joined:
    Nov 27, 2017
    Posts:
    6
    it only goes down just a further from the enemy ship, not far down at all.

    Yes I did. i also disabled game controller so It won't spawn more enemies and had only one enemy ship in the hierarchy to study it. It still has same problems even if mover is enabled. Although, if mover and evasive, both are enabled, then the enemy ship just stays up top where it spawned and moves(evades) on the x axis. The bolts won't come down regardless of the situation
     
  24. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Just be aware when you have an Enemy Ship in the scene/hierarchy, they will not move down the z-axis for some reason, even when the GameController and Mover script enable. I believe it have something to do with the prefabs, but I might be wrong on this. Try to test if your spawn Enemy Ships are moving down the z-axis and performing their evasive skills, yet leave the hierarchy Enemy Ship to see how they don't move down the z-axis.

    As for your enemy bolt, does it look like it shooting itself, like a reverse effect?
     
  25. zeen0

    zeen0

    Joined:
    Nov 27, 2017
    Posts:
    6
    No it doesn't shoot itself. The bolt speed is much more than the enemy ship's speed. I can see the bolt leaving and maybe traveling one length of the ship out. but then vanishes from the screen. This happens every time the ship fires.
    Also when the game controller spawns enemy ship, they move down (mover: enabled, evasive: disabled). And when i enable both (mover and evasive) It just stays up on the spawn point, evading on only the x axis
     
    gr8crE8r likes this.
  26. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    This is beyond my knowledge, I'm trying to think what cause this problem. Have you by any chance attach Destroy by Time script to Enemy Bolt prefab? I manage to replicate this by adding it onto the Enemy Bolt Prefab with a value of zero, and the bolt get destroy after being instantiated. As for your Evasive Script, I'm still looking around.

    Edit: Your Evasive Script seem to be fine, however I did found something unrelated.

    Where you wrote
    Code (CSharp):
    1. targetManeuver = Random.Range(1, dodge) * Mathf.Sign(transform.position.x);
    You forgot to write a - sign on Mathf.Sign, without it, your enemy ship will just keep evading to right only.

    Code (CSharp):
    1. targetManeuver = Random.Range(1, dodge) * -Mathf.Sign(transform.position.x);
    I feel I'm out of option to help you further, hopefully someone will help you out soon. Please do keep me updated when the issues are resolve so I too can learn something from this.
     
    Last edited: Jan 18, 2018
  27. leusid

    leusid

    Joined:
    Jan 3, 2018
    Posts:
    1
    Did you ever figure this out? You have some stuff in there that I don't and that I didn't see in the tutorial, such as IEnumerator, yield, and stuff... I'm at the point where I'm generating multiple asteroids, and it seems like whenever two spawn within each other's bounds, they interfere with each other, moving horizontally, not exploding when they're destroyed, sometimes not having their collider at the proper spot, plus most of the problems you mentioned. Not sure what's happening; this is the first time my results have deviated from the tutorial and there doesn't seem to be any official acknowledgement of the problem that I can find.
     
  28. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    @zeen0
    Is there any chance your enemy bolt gravity is check? If so, uncheck it so gravity won’t pulll your enemy bolt through the background.
     
    Last edited: Jan 18, 2018
  29. cjstaley1912

    cjstaley1912

    Joined:
    Jan 18, 2018
    Posts:
    1
    This helped a lot! Thanks!
     
    retsofh likes this.
  30. EvilWeevil

    EvilWeevil

    Joined:
    Jan 16, 2018
    Posts:
    16
    I am doing the Space Shooter tutorial, and have run into a problem with the -tile command, it's not working and isn't recognised in MonoDevelop, and I can't find it on the Unity Scripting API search:

    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class Boundary
    {
    public float xMin, xMax, zMin, zMax;
    }

    public class PlayerController : MonoBehaviour
    {

    public float speed;
    public Boundary boundary;

    private Rigidbody rb;

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

    void FixedUpdate ()
    {
    float movehorizontal = Input.GetAxis ("Horizontal");
    float movevertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (movehorizontal, 0.0f, movevertical);
    rb.velocity = movement * speed;

    rb.position = new Vector3
    (
    Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    );

    rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    }
    }
    This final line of code doesn't seem to want to do anything, and shows up as an error:

    Assets/Scripts/PlayerController.cs(38,64): error CS0103: The name `tilt' does not exist in the current context

    If I change the rb.velocity.x * -tilt to 0.0f, everything runs fine, but no tilt.

    Please help!
     
  31. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    The error message:
    Assets/Scripts/PlayerController.cs(38,64): error CS0103: The name `tilt' does not exist in the current context

    tells you the problem; the variable named "tilt" doesn't exists (it hasn't been defined)

    You need to add the line:
    public float tilt;

    at the top of the script, immediately under the lines:
    public float speed;
    public Boundary boundary;

    This will also mean that a slot for tilt will show up in the inspector when the player game object is selected
     
  32. KLN_TBN

    KLN_TBN

    Joined:
    Nov 26, 2017
    Posts:
    5
    Hi there, I'm getting this error in the console everytime an enemy bolt fires: "Non-convex MeshColliders with non-Kinematic rigidbodies are no longer supported in Unity 5''. I tried making the collider convex but I'm not sure how to, would doing this fix the error? If so, how would this be done?
    By the way, I deployed this game to android and implemented player-reward ads, a main menu, unlockable ships and some more daring stuff if you want to check it out: https://play.google.com/store/apps/details?id=com.Bolhetti.SpaceShooter
    I appreciate any answers in advance and would love to see more downloads or just ratings, if you have the time. Thanks!
     
  33. KLN_TBN

    KLN_TBN

    Joined:
    Nov 26, 2017
    Posts:
    5
    Hi there MORVOC, I decided to make a variant on this game for android to and ran into the same problem. The way I solved it was if the player wants to move left, they tap the left side of the screen. If they want to move right, they tap the right side of the screen. I implemented this idea in C# using this code:
    Code (CSharp):
    1. if
    2. (Input.GetTouch(0).position.x >= 200) {
    3.     GetComponent<RigidBody>().velocity = new Vector3 (speed, 0, 0);
    4. }
    5. else if (Input.GetTouch(0).position.x <= 200) {
    6.     GetComponent<RigidBody>().velocity = new Vector3 (speed, 0, 0);
    7. }
    '200' is the middle of my screen in pixels but it differs for different mobile devices so it may be smarter to use:
    Code (CSharp):
    1. int screenMiddleX = Screen.width / 2
    And replace 'screenMiddleX' with '200'.
    If you (or anyone reading this) want to see this functionality in action please check out my improvement of this tutorial game for android by clicking this link on your android mobile and hitting download, if you have the time: https://play.google.com/store/apps/details?id=com.Bolhetti.SpaceShooter If you don't have the time, I would appreciate a 5 rating (that should only take 10 seconds if you're used to Google Play). Anyways I hope this answer helped you MORVOC.
     
  34. KLN_TBN

    KLN_TBN

    Joined:
    Nov 26, 2017
    Posts:
    5
    Hi there MORVOC, I decided to make a variant on this game for android to and ran into the same problem. The way I solved it was if the player wants to move left, they tap the left side of the screen. If they want to move right, they tap the right side of the screen. I implemented this idea in C# using this code:

    Yep, worked for me: https://play.google.com/store/apps/details?id=com.Bolhetti.SpaceShooter
     
  35. EvilWeevil

    EvilWeevil

    Joined:
    Jan 16, 2018
    Posts:
    16
    *Face palm moment* Of course, thanks!!!!!!
     
  36. EvilWeevil

    EvilWeevil

    Joined:
    Jan 16, 2018
    Posts:
    16
    Hi.

    Whilst doing the Boundary section of the Space Shooter tutorial I have encountered an issue. I followed all steps, and scripting, the boundary is set up, but the boundary does not destroy the Bolt, and the Bolt remains in the Hierarchy menu until the game is stopped. The bolt then disappears from the Hierarchy menu (as before the boundary was made, as the Bolt is only there as a prefab. Here is the script I am using for the Boundary:

    using UnityEngine;
    using System.Collections;

    public class DestroyByBoundary : MonoBehaviour
    {
    void OnTriggerExit(Collider other)
    {
    Destroy(other.gameObject);
    }
    }

    I have checked the upgrade guide for Unity 5 (which has worked for everything else up until this point), but in this section it doesn't report any problems.

    What am I doing wrong? I have checked, re-checked all the steps about 3 times. Deleted and recreated the Boundary 3 times as well, but still it isn't destroying the Bolt prefab.

    Thanks
     
  37. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Try replacing the line
    Destroy(other.gameObject);
    with
    Object.Destroy(other.gameObject);

    and see if it makes a difference.

    If not, maybe try:
    Code (csharp):
    1.  
    2. public class DestroyByBoundary : MonoBehaviour
    3. {
    4.     void OnTriggerExit(Collider other)
    5.     {
    6.         if (other.transform.parent == null)
    7.         {
    8.             Object.Destroy(other.gameObject);
    9.         }
    10.         else
    11.         {
    12.             Object.Destroy(other.transform.parent.gameObject);
    13.         }
    14.     }
    15.  
    16. }
    17.  
     
  38. EvilWeevil

    EvilWeevil

    Joined:
    Jan 16, 2018
    Posts:
    16
    If not, maybe try:
    Code (csharp):
    1.  
    2. public class DestroyByBoundary : MonoBehaviour
    3. {
    4.     void OnTriggerExit(Collider other)
    5.     {
    6.         if (other.transform.parent == null)
    7.         {
    8.             Object.Destroy(other.gameObject);
    9.         }
    10.         else
    11.         {
    12.             Object.Destroy(other.transform.parent.gameObject);
    13.         }
    14.     }
    15.  
    16. }
    17.  
    [/QUOTE]

    This worked, thank you. What is the reason for it working this way, and not the other way?
     
  39. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    This worked, thank you. What is the reason for it working this way, and not the other way?[/QUOTE]

    I'm not sure. I modified my code to be:
    Code (csharp):
    1.  
    2. public class DestroyByBoundary : MonoBehaviour
    3. {
    4.     void OnTriggerExit(Collider other)
    5.     {
    6.         if (other.transform.parent == null)
    7.         {
    8.             Debug.Log("DestroyByBoundary.OnTriggerExit. other.transform.parent == null, other.name="+other.name);
    9.             Object.Destroy(other.gameObject);
    10.         }
    11.         else
    12.         {
    13.             Debug.Log("DestroyByBoundary.OnTriggerExit. other.transform.parent != null, other.transform.parent.name=" + other.transform.parent.name);
    14.             Object.Destroy(other.transform.parent.gameObject);
    15.         }
    16.     }
    17.  
    18. }
    19.  
    Here is the result of the game condole:
    upload_2018-1-26_11-20-59.png

    Note "Bolt" (both mine and enemy) don't have parent (though asteroid does)
     
  40. ResExUnity

    ResExUnity

    Joined:
    Jan 26, 2018
    Posts:
    1
    Hello!

    I am quite new to C#, but I have knowledge of C++ and Python, so I am familiar with object-oriented programming.

    Right now I am trying to figure out collision detection between an asteroid and my ship; the asteroid explodes when I fire a laser at it, but when I run my ship over it, nothing happens, and both overlap. Here's what I've tried:

    • Made both the ship's and asteroid's colliders as triggers. Didn't help
    • Made only the ship's collider as a trigger. Didn't do anything, either
    • Tried it with only the asteroid's collider as a trigger. Failed
    • Made neither of the colliders triggers.
    • Created my own tag for the ship and edited the code. Nothing
    With Debug.Log I noticed that when I ran my ship over the asteroid, nothing happened, which is weird as one of the times I did that, both objects' collider's were triggers.

    Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestroyByContact : MonoBehaviour
    6. {
    7.      public GameObject explosion;
    8.      public GameObject playerExplosion;
    9.  
    10.      void OnTriggerEnter(Collider other)
    11.      {
    12.          if (other.gameObject.CompareTag ("Player"))
    13.          {
    14.               Instantiate (explosion, transform.position, transform.rotation);
    15.               Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    16.  
    17.               Destroy (other.gameObject);
    18.               Destroy (gameObject);
    19.          } else {
    20.               Instantiate (explosion, transform.position, transform.rotation);
    21.  
    22.               Destroy (other.gameObject);
    23.               Destroy (gameObject);
    24.          }
    25.      }
    26. }

    Also, as part of an attempt to fix that bug, I replaced that boundary cube with two smaller cubes on either end of the play area, so there was no need to do that if (other.tag == "Boundary") thing

    Thanks!
     
  41. bilalakil

    bilalakil

    Joined:
    Jan 28, 2018
    Posts:
    77
    Hi there, I'm actually stuck right at the beginning: I can't figure out what/how to import the assets.

    I've followed the link on the Space Shooter tutorial page that takes me to Space Shooter tutorial in the asset store. However, after downloading that, I'm warned that opening a new project will over completely overwrite the current project.

    That's not what happens in the video at all. The instructor creates a new project from scratch and imports various assets into it, instead of overwriting the entire project.

    What am I doing wrong here? Thanks.
     
    kberck3 likes this.
  42. Zuzukal

    Zuzukal

    Joined:
    Jan 23, 2018
    Posts:
    2
    Hello there, I've been working on the Space Shooter tutorial for some time now and am running into issues with the Extended Tutorial. While creating the enemy bolts, making them move, and giving them a fire rate I seem to have broken the fire rate somewhere. A shot is fired once the game starts up and a second right after but that's it. Increasing or decreasing the value in the Fire Rate field does nothing and there doesn't seem to be any errors in my code.

    I am assuming the error would have to be within the WeaponController.cs. I'm new to coding and have queried for days with no luck. Help would greatly be appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponController : MonoBehaviour
    6. {
    7.     private AudioSource audioSource;
    8.  
    9.     public GameObject shot;
    10.     public Transform shotSpawn;
    11.     public float fireRate;
    12.     public float delay;
    13.  
    14.     void Start ()
    15.     {
    16.         audioSource = GetComponent<AudioSource>();
    17.         InvokeRepeating("Fire", delay, fireRate);
    18.     }
    19.  
    20.     void Fire()
    21.     {
    22.         Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    23.         audioSource.Play();
    24.     }
    25. }
     
  43. nwzebra

    nwzebra

    Joined:
    Dec 4, 2015
    Posts:
    16
    Nothing, just keep going. Create a new project, import the assets. At this point you have a empty project so you don't care if setting are overwritten.
     
  44. eldevin4

    eldevin4

    Joined:
    Feb 4, 2018
    Posts:
    1
    Hey, can someone help me? I can't see the GUI-Text in the Game and I don't know what I have done wrong.

    Edit I am now using UI Text works fine but it would be cool to know what went wrong with GUI-Text
     

    Attached Files:

    • GUI2.PNG
      GUI2.PNG
      File size:
      776.8 KB
      Views:
      1,045
    Last edited: Feb 5, 2018
  45. bdilloughery

    bdilloughery

    Joined:
    Jun 19, 2017
    Posts:
    16
    Can't save my project.

    I am moderately experienced in Unity, but cannot for the life of me save my Space Shooter project.

    I began the project by clicking on the "Learn" tab, selecting the tutorial, clicking the "Download" button and then clicking the "Start" button.

    I worked on the tutorial the other night for a few hours. For sure saved it, had my main scene saved, prefabs saved. All was saved. Closed it. It doesn't exist anywhere on my computer. I can't find the project, the scene, anything.

    The only remaining hint that I used it, was that when I click on the "Learn" area when Unity opens, the tutorial is downloaded and has a "Start" button.

    I am currently working on it again. I have saved the project and the scene many times. I have the project open. I cannot find the project folder or any of its assets anywhere on my computer.

    Since there is no "Save as..." option for projects, I can't just tell Unity to save the project to a folder in my desired location. I can't find the folder anywhere on my computer (this is a pretty clean, new OS). I understand that I could export the entire project and then import it, but then I would have to reconfigure settings.

    How can I save this project? Where on my PC is this project operating from when I load up the tutorial? It isn't in the same folder as every other Unity project I've created and it isn't in the Program Files/Unity folder as best that I can tell.

    Thank you,
    Brendan

    ----------------------------------------------------------------------------------------------------------------
    Edit:

    Still, the same problem but just realized the bar at the top of my project window says the project I am working on is "New Unity Project" no matter how many times I try to save it.

    Capture.PNG


    ----------------------------------------------------------------------------------------------------------------
    SOLVED (sort of):

    Here was a non-fix:
    Second time i made the project, i waited until totally complete and after I had the build I wanted ... and then when closing the project (even though I had selected "Save Project" many times), it asked if I wanted to "Keep" the project and told me otherwise all changes would be discarded. I selected to keep the project and it came up with a file moving error and just asked (try again) (force quit) or (cancel).

    Here was the fix:
    1. Added this line of code to a script to see where the project folder was on computer:
    Debug.Log (Application.dataPath);
    2. Folder was in C:/Users/username/AppData/Local/Temp/crazyName
    ... the name was a 32 character mix of numbers and letters. had to unhide hidden folders just to go into the AppData folder
    4. I closed Unity (otherwise you can't copy and paste this folder while in use)
    5. Copied this 32-character-named folder to a clear location on my computer and renamed it to "Space Shooter"
    6. Opened Unity and selected the "Open Project ..."


    Using Unity Version 2017.3.of3 Personal on 64 bit Windows 10
     
    Last edited: Feb 8, 2018
  46. bdilloughery

    bdilloughery

    Joined:
    Jun 19, 2017
    Posts:
    16
    Your font size says zero in the pic. One more thing - if you make the font too big to fit into the UI Text box dimensions, it disappears. You may need to enlarge the UI Text box dimensions if your text isn't showing up.
     
  47. zakperic

    zakperic

    Joined:
    Feb 7, 2018
    Posts:
    1
    HI People,

    I am trying to learn Unity. I followed the Shooter tutorial, currently I have Personal version of Unity 3.0f3 installed on my mac. When it got to writing the first part of the script I got stuck. Basically there is an error which I can not fix at all.

    Can someon help please.
    What am I doing wrong.


    ------------------

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    GetComponent.velocity = movement;
    }
    }
    --------------------------
     
  48. bdilloughery

    bdilloughery

    Joined:
    Jun 19, 2017
    Posts:
    16
    Your getting the rigidbody (physics component incorrectly).

    At the top (inside your class, but outside of FixedUpdate):
    Rigidbody rbody;

    void Awake() {
    rbody = GetComponent<Rigidbody>();
    }


    with these lines of code, you now have "rbody" referencing the Rigidbody of the GameObject the script is attached to. From then on you can use "rbody"

    so you can replace "GetComponent.velocity = movement" with "rbody.velocity = movement"
     
  49. Inteligirl

    Inteligirl

    Joined:
    Jan 17, 2018
    Posts:
    1

    Thanks for this. I was really having a hard time with the text showing up!
     
  50. rugvedkhandekar

    rugvedkhandekar

    Joined:
    Feb 8, 2018
    Posts:
    6
    I was following the 'Shooting Shots' tutorial. When I started doing stuff in my project, there was no Player Controller Script in the player object as in the video. Hence I created a new C# script for the playerShip. I copy-pasted the script given below the video in my new 'PlayerControllerScript'. But now there are some new errors. The errors are:

    1. 'UnityEngine.Component' does not contain a definition for 'velocity'.

    2. 'UnityEngine.Component' does not contain a definition for 'position'.

    I would appreciate your help. Thanks in Advance.