Search Unity

Space Shooter Tutorial Q&A

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

  1. Kav_sub

    Kav_sub

    Joined:
    Jul 7, 2015
    Posts:
    2
    I'll go ahead and do that, but I don't understand why the speed variable should be necessary to move the ship. Yes, the ships movement will be slower without it, but it should be moving nonetheless. Right now it seems more like an issue of input detection. I'll try what you suggested out and get back to you, thanks!

    EDIT: Tried it out, still nothing.

    EDIT 2: I went through the input settings again, fiddled around a bit and it started working.
     
    Last edited: Aug 27, 2015
    IanSmellis likes this.
  2. IvarvandenBerg

    IvarvandenBerg

    Joined:
    Mar 11, 2015
    Posts:
    5
    Hi, i'm trying to go a little further than the tutorial and everytime a new wave starts i would like it to spawn one extra asteroid. I've been trying a couple of different thing but none of them seem to work. I tried to make it so that after every wave finished a waveCounter would go +1. this I tried to inmplement in the FOR loop so that it would count to the hazardCount + waveCounter, starting at a hazardCount of 10.

    Since the waves get spawned in a coroutine i can't seem to figure out how to make a waveCounter that works.

    Any suggestions?
     
  3. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @R42,

    Firstly, are you getting any errors coming up in the console window (should show in bottom right if you are)

    also, can you post a copy of your PlayerController script please for a look..
    (but please use code tags when posting scripts please, the icon just left of save in the top bar when you post ;))
     
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @IvarvandenBerg,

    Im just back from a couple of weeks work and had to have a tinker with Unity again, so here goes :)

    Just to give you an idea to get the ball rolling so to speak,
    within your PlayerController script, at the top where you have your private variable declarations, add the following
    Code (CSharp):
    1. // initially sets your wave Count to 1 when the scene loads or reloads
    2.     private int waveCount = 1;
    so use that as a wave counter.

    then within the coroutine, where you wave your waveWait co routine call, just above it add the following
    Code (CSharp):
    1.  
    2. hazardCount++; // adds one more baddie to the game
    3. waveCount++; // while your there add one to your wave counter
    4. Debug.Log ("Wave : " + waveCount); // print to console just to see
    5. yield return new WaitForSeconds (waveWait); // you have this line already!! just for reference
    6.  

    should do the Job ;)
     
  5. R42

    R42

    Joined:
    Jun 27, 2015
    Posts:
    4
    Hi OboShape.
    My console is silent - no errors or warnings.
    Here is the 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.     public float speed;
    14.     public Boundary boundary;
    15.     public float tilt;
    16.  
    17.     public GameObject shot;
    18.     public Transform shotSpawn;
    19.  
    20.     public float firerate;
    21.     private float nextFire;
    22.  
    23.     void Start() {
    24.         rb = GetComponent<Rigidbody> ();
    25.     }
    26.  
    27.     void Update() {
    28.         if (Input.GetButton("Fire1")&&Time.time > nextFire){
    29.             nextFire = Time.time + firerate;
    30.             //GameObject clone =
    31.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation); //as GameObject;
    32.         }
    33.     }
    34.  
    35.     void FixedUpdate() {
    36.         float moveHorizontal = Input.GetAxis ("Horizontal");
    37.         float moveVertical = Input.GetAxis ("Vertical");
    38.  
    39.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    40.         rb.velocity = movement *speed;
    41.  
    42.         rb.position = new Vector3
    43.             (Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    44.                           0,
    45.                           Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax));
    46.  
    47.         rb.rotation = Quaternion.Euler (0,0,rb.velocity.x * -tilt);
    48.     }
    49. }
    50.  
    I probably made an embarrasing error somewhere but after all day of messing with 3ds max and Unity I'm hardly thinking ;)

    Thanks in advance.
     
  6. IvarvandenBerg

    IvarvandenBerg

    Joined:
    Mar 11, 2015
    Posts:
    5
    Thanks @OboShape! Worked like a charm! Thought it would be a nice touch. Probably gonna try and see how I can make the game a bit more interesting so there's a big chance i'll be back with another question soon.
     
    OboShape likes this.
  7. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Cheers for posting that, as far as i can see codes fine, can you have a look through your game objects, like bolt, boundary, player etc and double check the components and the transforms are the same as the tutorial, i know its going to be a long winded check, but sometimes its not the code thats causing issues.

    ive been trying to recreate it, but cant for the life of me think what could be causing that behaviour.
     
  8. lelapinblanc

    lelapinblanc

    Joined:
    Aug 27, 2015
    Posts:
    3
     
  9. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Not sure what your posting here.
    can you please edit and post a question please.
     
    Last edited: Aug 27, 2015
    IanSmellis likes this.
  10. R42

    R42

    Joined:
    Jun 27, 2015
    Posts:
    4

    After long investigation I've finally realised what caused the problem. I compared my bolt with done_bolt and it seems that I copied part of the code from the first post to the mover script when I wasn't supposed to.. :confused:
    Like I said: an embarassing error.

    Code (CSharp):
    1. public class Mover : MonoBehaviour {
    2.  
    3.     private Rigidbody rb;
    4.     public float speed;
    5.  
    6.     void Start ()
    7.     {
    8.         rb = GetComponent<Rigidbody>();
    9.         rb.velocity = transform.forward * speed;
    10.     }
    11.  
    12.     void Update ()
    13.     {
    14.         if (Input.GetButton("Fire1"))
    15.         {
    16.             rb.velocity = Random.insideUnitSphere;
    17.         }
    18.     }
    19.  
    20. }
    Without update part everything works like a charm. Thanks for assistance.
     
    IanSmellis likes this.
  11. IvarvandenBerg

    IvarvandenBerg

    Joined:
    Mar 11, 2015
    Posts:
    5
    Another question, I'm trying to make the asteroids spawn closer to eachother after each wave. Right now they are stable at a random.Range between 0.25 and 0.5 secconds. I tried to add some code at the end of my coroutine which seems like the right place but it won't let me substract a value, it only alows a -- which automaticly sets it to de bottom value.

    It just gives me the error: Parse error: Only assignments, call, increment, decrement, await, and new object expressions can be used as a statement.
    This is the code i'm trying to write


    Code (CSharp):
    1.     if (spawnWaitMax > 0.3)
    2.     {
    3.  
    4.                     spawnWaitMax - 0.01;
    5.                 }

    Thanks in advance,
    Ivar
     
  12. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    (assuming spawnWaitMax is a float....)
    you aren't assigning this to anything, try
    Code (CSharp):
    1.  
    2. if (spawnWaitMax > 0.3f)
    3. {
    4.      spawnWaitMax = spawnWaitMax - 0.01f;
    5. }
    6.  
    or shorthand that line to
    Code (CSharp):
    1. spawnWaitMax -= 0.01f;
     
  13. bezceller

    bezceller

    Joined:
    Aug 31, 2015
    Posts:
    3
    Hello.
    I have a problem with a lesson 10 when adding the Mover script to the asteroid. The asteroid begins to move in random direction according to it rotation. can be it because of rotator script applyed? - it rotates gameobject and its z-axis is rotating too.
    On the screenshot is showed the simulation stopped - x and y coordinates are not 0 as they are set for gameobject.
    windowScreenshot.png

    Using Unity 5.0.4f1 (Unity 5.1 crashes at launch)

    p.s. i'm newbie in Unity, C and programming at all =)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mover : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     private Rigidbody rb;
    9.  
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody> ();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         rb.velocity = transform.forward*speed;
    18.     }
    19.  
    20. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomRotator : MonoBehaviour
    5. {
    6.     public float tumble;
    7.  
    8.     private Rigidbody rb;
    9.  
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody> ();
    13.         rb.angularVelocity = Random.insideUnitSphere*tumble;
    14.     }
    15.  
    16.  
    17. }
     
  14. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon @bezceller,

    If you look at your Mover script.
    you should be setting the velocity only once, and this should be done in the Start() method.

    where you have it currently is in Update(), whereby it will set the velocity each time a frame is drawn.
     
  15. Uncivilization

    Uncivilization

    Joined:
    Aug 30, 2015
    Posts:
    5
    Can someone tell me why the private Rigidbody rb; won't work for me?

    I get these errors

    I also was only able to move if I was to use
    Code (CSharp):
    1. GetComponent<Rigidbody>().velocity = movement * speed;
    but I wanted to update it, to only have to enter "rb"

    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.  
    14.     public float speed;
    15.     public Boundary boundary;
    16.  
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody> ();
    20.     }
    21.  
    22.     void Update ()
    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. }
     
  16. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon,
    the vector 3 components should be in () braces not {} braces. edit to show like below, should be good :)
    Code (CSharp):
    1.  rb.position = new Vector3
    2.         (
    3.             Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    4.             0.0f,
    5.             Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    6.         );
    one other wee thing..
    you have
    Code (CSharp):
    1. void Update ()
    best to change this to seen as your altering physics components.
    Code (CSharp):
    1. void FixedUpdate ()
     
    DubbyBoo and Uncivilization like this.
  17. Uncivilization

    Uncivilization

    Joined:
    Aug 30, 2015
    Posts:
    5
    Thanks, changed the FixedUpdate and Vector3 - Sometimes a second pair of eyes help :)

    fist bump
     
    OboShape likes this.
  18. bezceller

    bezceller

    Joined:
    Aug 31, 2015
    Posts:
    3
    so easy... thanks =)

    and what code should i use to move gameobject in scene coordinates? say, the asteroid, which is already rotated
     
    Last edited: Sep 1, 2015
  19. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    For the Space Shooter, Asteroid and Player you are making the physics engine do all the work for you.
    what this basically means is you are setting an initial velocity only once, giving them an initial speed and direction and the engine handles the movement after that. imagine throwing a ball, you only throw it once and it takes care of its own movement after that.

    if your wondering how to move/translate objects that you are not using with physics, you can have a wee look under the tutorial section as theres loads of good vids there.
    http://unity3d.com/learn/tutorials/modules/beginner/scripting/translate-and-rotate?playlist=17117
    or this one, i think it covers transforms and coordinates (i think)
    http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/gameobjects
     
  20. bezceller

    bezceller

    Joined:
    Aug 31, 2015
    Posts:
    3
    thanks for links.

    i just ended Space Shooter tutorial and now i'm making some changes. I tried to move Player only forward according to gameobject rotation among y-axis and i understood, why our Player gameobject in tutorial was moving exactly as we want - by z and x axis, but Asteroid was not (when velocity was applied each frame in Update function, i mean)... it was because each time we set Player velocity as fresh new Vector3, which is defined in global coordinates =) it is a real discover for me =) but it's really basics, i understand
    i'll finish all tutorials i can before asking any questions next time.
     
  21. IvarvandenBerg

    IvarvandenBerg

    Joined:
    Mar 11, 2015
    Posts:
    5
    @OboShape

    Thanks again! That was indeed the problem. Just out of curiosity, how would I have written this code if spawnWaitMax had been an integer?

    Sincerely,
    Ivar


    edit: As a last touch I would also like to up the speed a little each wave. However the speed is controlled in a sepparate script attatched to the asteroid object.

    Code (CSharp):
    1. public class Mover : MonoBehaviour {
    2.  
    3.     public float speed;
    4.     private Rigidbody rb;
    5.    
    6.     void Start ()
    7.     {
    8.         rb = GetComponent<Rigidbody>();
    9.    
    10.         rb.velocity = transform.forward * speed;
    11.    
    12.     }
    13. }
    I would like to be able to change the speed variable in my gameController script, only I do not exactly now how to call it there.
     
    Last edited: Sep 1, 2015
  22. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    IMO spawnwaitmax as you have it, needs to be a float as you will run into problems when dealing with indifferent types (ie trying to subract a float from an int and then trying to assign it) and will need some casting.
    floats give you the decimal accuracy, ints do not.

    theres a few different ways you could work around increasing speeds. you could make a new mover script for asteroids, pop a static float in there for the speed variable.
    then from within the script that tells you when a wave is complete you can reference your script and then increase the speed on the class directly, which will affect every instance.

    for info on statics look here.
    http://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics?playlist=17117
     
  23. kmj1996

    kmj1996

    Joined:
    Sep 1, 2015
    Posts:
    8
    I have error like this
    c:\Users\Public\Documents\Unity Projects\Space Fighter\Assets\Scripts\PlayerController.cs(13,13): Error CS1061: 'UnityEngine.Component' does not contain a definition for 'velocity' and no extension method 'velocity' accepting a first argument of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp)

    please give me answer...... i want help
     
  24. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    can you post your PlayerController script please as we cant help any further without having a look.
    At first glance, I would think that your Rigidbody isn't set up correctly, but pop your script up, and we can look further.
     
  25. kmj1996

    kmj1996

    Joined:
    Sep 1, 2015
    Posts:
    8
    error.png
     
  26. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     // variable declarations
    7.     public float speed; // remember to set this in inspector or it wont move at all!!
    8.  
    9.     void FixedUpdate()
    10.     {
    11.         float moveHorizontal = Input.GetAxis ("Horizontal");
    12.         float moveVertical = Input.GetAxis ("Vertical");
    13.      
    14.         Vector3 movement= new Vector3 (moveHorizontal, 0.0f, moveVertical);
    15.         GetComponent<Rigidbody>().velocity = movement * speed;
    16.     }
    17. }
    if you look at line 15 above.
    you need to use GetComponent as you can no longer use the DOT operator to access the rigidbody
    (enable annotations in tutorial videos to see unity 5 updates and info like this)

    also, set the speed variable in the inspector when you return to the editor, same as Adam had in tutorial so you can adjust speed without going into the code.
     
  27. kmj1996

    kmj1996

    Joined:
    Sep 1, 2015
    Posts:
    8
    Thanks...... its work but still player vehicle not move
    but your code work no error occur
     
  28. kmj1996

    kmj1996

    Joined:
    Sep 1, 2015
    Posts:
    8
    sorry sorry
    its my fault
    now its move
    everything is ok
    thanks again
     
  29. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    don't apologise, its a learning curve :)

    good stuff you got it going.. i take it you forgot about the speed ;)
     
  30. mmcreations

    mmcreations

    Joined:
    Sep 1, 2015
    Posts:
    6
    I have a little Problem in Section 2.03 (Explosions). When I add the "Move.cs" to the Asteroid, the Asteroid moves for about two seconds and then it destroys himself in the middle of the playfield abruptly - without any collision.

    Can anybody help, please? :)
     
  31. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    little tricky to guess, but there's bound to be something destroying it like the boundary at first thought.
    when you say that the asteroid destroys itself, does it play the explosion or just disappear?

    do they always dissapear at the same distance down the scene when you play?

    if you disable the Boundary in the scene, just to test, do the asteroids still vanish?
     
    Last edited: Sep 1, 2015
  32. mmcreations

    mmcreations

    Joined:
    Sep 1, 2015
    Posts:
    6
    1) The asteroid just disappear, without any explosion.

    2) Its tricky. Because the Astroids moving in a curve, bevor he disappear.

    3) If I disable the Boundary, the Asteroid don't disappear, but it moves sideways - in curves.
     
    Last edited: Sep 2, 2015
  33. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    can you post your Move.cs script please for a look
     
  34. mmcreations

    mmcreations

    Joined:
    Sep 1, 2015
    Posts:
    6
    Move.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class Mover : MonoBehaviour
    7. {
    8.      public float speed;
    9.    
    10.     private Rigidbody rb;
    11.    
    12.     void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody> ();
    15.     }
    16.    
    17.     void Update()
    18.     {
    19.         rb.velocity = transform.forward*speed;
    20.     }
    21.    
    22. }
     
  35. mmcreations

    mmcreations

    Joined:
    Sep 1, 2015
    Posts:
    6
    I have select "Freeze Position" on Axis Y (Asteroid - Rigidbody). So the Astroid cant collide with the bottom of the boundary. That solves the problem 1. The other two problems are still there. So the Astroid moves sometimes sideways, but always in strange curves.
     
  36. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    @mmcreations,

    the velocity should be set only once.
    Delete the Update() method and put the code at the bottom of your Start() method.
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         rb = GetComponent<Rigidbody> ();
    4.         rb.velocity = transform.forward*speed;
    5.     }
    With what you had in the Update, it was setting the velocity on each frame.

    see how that goes.
     
  37. mmcreations

    mmcreations

    Joined:
    Sep 1, 2015
    Posts:
    6
    It works and I learned something ;)
    Thank you very much.
     
  38. cnorth

    cnorth

    Joined:
    Sep 2, 2015
    Posts:
    1
    Hi, I'm new to Unity and started with the Space Shooter tutorial. Lots of fun!!!

    Just wondering if there is a subsequent or advanced tutorial as to add more features to the game, like the enemies part?

    Thanks!
     
  39. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon @cnorth,

    There is no official additions to this tutorial, however.. There is still much to learn from within the project :)

    if you look in the 'Done' folder of the project, all the scripts and prefabs are there for adding more enemies and their behaviours etc.

    These were included by Adam and the learn team for you to dissect and learn from at your leisure as 'Stretch Goals'.
     
  40. pwn_dhami

    pwn_dhami

    Joined:
    Sep 2, 2015
    Posts:
    5
    when i try to open the MonoDevelop there is an error says that : while trying to load the project 'C:\users\sai\Desktop\Space Shooter\Assembly-CSharp.csproj': Object reference not set to an instance of an object.
    please help to figure out this problem.
     
  41. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Is this an error showing in Unity, or just when you open MonoDevelop?

    I take it you opened monodevelop via unity editor.
    If you close Mono down, reopen Mono by itself (not via editor), and then try opening a script via unity in your shooter project, does that make any difference?

    or open MonoDevelop, and File > Open and browse to where you Shooter project files are, see if it opens that way.
     
  42. pwn_dhami

    pwn_dhami

    Joined:
    Sep 2, 2015
    Posts:
    5
    Thanks for your kind suggestion it works .
     
    OboShape likes this.
  43. PANDEMICBANDIT

    PANDEMICBANDIT

    Joined:
    Aug 31, 2015
    Posts:
    8
    I'm having a small issue in the space shooter tutorial. And I believe its something that can easily be fixed and I'm not noticing it. But I have all the spawn settings fixed exactly like in the tutorial but I'm only getting one asteroid to spawn.

    Anyone might know what the problem might be?.
    Thanks.
     
  44. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning,
    Have you set the HazardCount value in the inspector of your GameController in your scene?
    double check the other values in the inspector as well please, just while your there :)

    pop a copy of your GameController script in here please for a wee look.
     
    Last edited: Sep 4, 2015
  45. Kyrros1

    Kyrros1

    Joined:
    Jul 31, 2015
    Posts:
    3
    I can't figure this one out. Everything's working except for asteroids being removed from the Hierarchy list. They only are removed when I place the DestroyByTime script on them and in no other circumstance.

    Both debug lines are being triggered. DestroyByTime cleans up explosions nicely. The boundary cleans up bolts just fine. Shooting or colliding with asteroids destroys the bolt, asteroid, explosion(s), and/or ship as intended but still does not remove the asteroid from the Hierarchy window.

    Attached to the child of the asteroid prefab (w/explosion assets attached):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour {
    5.  
    6.     public GameObject explosion;
    7.     public GameObject playerExplosion;
    8.     void OnTriggerEnter(Collider other){
    9.         if (other.tag == "Boundary")
    10.         {
    11.             Debug.Log (other.name);
    12.             return;
    13.         }
    14.         Instantiate (explosion, transform.position, transform.rotation);
    15.         if (other.tag == "Player") {
    16.             Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    17.         }
    18.         Destroy (other.gameObject);
    19.         Destroy (gameObject);
    20.     }
    21.  
    22. }
    Attached to the boundary:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByBoundary : MonoBehaviour {
    5.  
    6.     void OnTriggerExit(Collider other) {
    7.         // Destroy everything that leaves the trigger
    8.         Debug.Log (other.name + " DBB");
    9.         Destroy(other.gameObject);
    10.     }
    11. }
    12.  

    Attached to explosions:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByTime : MonoBehaviour {
    5.  
    6.     public float lifetime;
    7.     void Start (){
    8.         Destroy (gameObject, lifetime);
    9.     }
    10. }
    Any ideas?
     
  46. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning @Kyrros1,
    Just wondering, have you got the capsule collider on the Asteroid parent, and not on the child art asset?
     
  47. Kyrros1

    Kyrros1

    Joined:
    Jul 31, 2015
    Posts:
    3
    Tried both ways, same result. It's currently on the child and the parent is without any properties other than it's Transform which is set at 0,0,8.

    Edit: Thanks for the help, btw. It's a little frustrating having weird behavior like this. lol
     
    Last edited: Sep 4, 2015
  48. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    No worries :)
    Just watched over the tute vid again for creating the Hazards, and all the components are on the main Parent gameobject.
    and the child is just the art asset itself.
    Destroy(gameObject), destroys the gameObject that the script is attached to, which, if the script is attached to the child, its that that will get marked for destruction.
    so if you watch the scene heirarchy as your asteroid leaves the trigger volume, you will see the child being destroyed (as the turn down arrow will dissapear) but the parent is still there.
    to destroy the parent you will need to grab the parent object of the object that your script is on.
    Code (CSharp):
    1. Destroy(transform.parent.gameObject);
    or you could just pop all the components on the parent, just to keep in line with vid ;)
     
    IanSmellis likes this.
  49. Kyrros1

    Kyrros1

    Joined:
    Jul 31, 2015
    Posts:
    3
    That makes sense. Looks like it fixed the problem with the asteroids not being removed, but there's a whole slew of other problems that cropped up by moving the child properties to the parent. Troubleshooting time! :D

    Edit: All fixed, just needed a minor tweak. Working good, woo! Thanks again for the assistance, Obo. Much obliged.
     
    Last edited: Sep 4, 2015
    OboShape likes this.
  50. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    No bother, glad its working.
    At the end of the day, thats what these Fantabulous forums are for :)