Search Unity

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

Space Shooter Tutorial Q&A

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

  1. STYJ_VR

    STYJ_VR

    Joined:
    Jan 13, 2016
    Posts:
    2
    Hi, I've got a question regarding Mathf.Clamp.

    ATM, I've only done until 05 Moving the Player.

    I am supposed to Clamp the player to prevent it from moving out of the game screen. I've followed the video tutorial and changed where appropriate i.e. instead of using the shorthand handles, I created a Rigidbody variable. When trying to Clamp, I am still able to move past the edges albeit extremely slowly. I've tried looking up but to no avail. Any help will be much appreciated!

    This is my code.

    using UnityEngine;
    using System.Collections;
    public class PlayerController : MonoBehaviour {

    private Rigidbody rb;
    public Boundary boundary;
    public float speed;

    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.AddForce(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)
    );

    }

    }

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


    edit: I've looked through this thread and found my mistake. I'm supposed to use rb.velocity instead of rb.AddForce.
     
    Last edited: Jan 13, 2016
  2. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30
    Then make the player to not rotate also.

    I'm going to update the code on my previews post, check there.
     
  3. technano

    technano

    Joined:
    Jan 13, 2016
    Posts:
    23
    I've Run into a new error were pressing 'R' doesn't restart my game, I've checked and double checked my code and it's right as far as I can see!! Please help!!
    Code (CSharp):
    1. void update()
    2.     {
    3.         if (restart)
    4.         {
    5.             if (Input.GetKeyDown (KeyCode.R))
    6.             {
    7.                 Application.LoadLevel (Application.loadedLevel);
    8.             }
    9.         }
    10.     }
    This is the code I have and it's not doing anything.
    Also When I try typing .LoadLevel and .loadedLevel they've been redacted so I'm not to sure what that means either
     
    Last edited: Jan 14, 2016
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The resolution should depend upon the target device. This is a more complicated issue that can really be solved here. What would be best would be to ask in the appropriate mobile platform section (eg: iOS or Android). The camera should automatically adjust to the resolution, but you may need to prepare your UI for any changes in platform and target device.
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'll try to look into this. The code should be correct and should not generate any errors.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    So, a couple of points:

    You are creating hazards the move in random directions? And they can collide? As part of the design of the game, all of the hazards are moving in the same direction and at the same speed. This means that they cannot collide with each other. The hazards currently have, at least in the base project - I have no idea what you've done - a destroy by contact script that will destroy these objects when they collide with each other (or any other object), if I remember correctly. To prevent this, you'll need to either check the hazard's tag, or affect collision layers in the physics settings.

    If you have disabled Is Trigger on the colliders... Depending upon other factors, your hazards could be coming into contact with each other, not being destroyed (as they are no going to receive an OnTriggerEnter message) but the physics engine will knock them out of the play area due to having a physical collision. As they leave the play area, the Boundary will probably destroy them. Watch what happens in the Scene View when playing the game.

    SpawnWaves needs to be a coroutine if you are following the design of this game. This means it needs to return IEnumerator. I can't see how this can change your Instantiate code.

    Unity NameSpaces... I'm confused... What is it you need?
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please give us more information? Like, what the errors are? And what temporary / partial fix you've implemented? FWIW: the DestroyByLifetime code looks fine.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I can't seem to replicate this issue. Can you submit a bug report with your project attached and get me the number? This way I can have a look at your project. In the description, give a proper account of what the problem is, but also mention in the last line of the bug report that it needs to be for the attention of Adam Buckner in the R&D Content Team.
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Did you find a solution to your problem? Or is it still on-going?
     
  10. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30

    Done, Case 761600.


    Thanks for answer and clarification.
    The hazards seem to collide with each other and I think that's why they are getting random directions. So my option to do this on porpouse is to change the Mover.cs ?

    About the Unity NameSpace I was talking about the "using UnityEngine;" I would like to know If there is a way to check the funtions inside this namespace, not the documentation but the funtions itself.
     
  11. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30
    Forgot to mention, I already finish this tutorial/project, and I encounter another little problem.

    Some EnemyBolts seem to scape from the boundary, probably as they collide somehow with something (I have followed them and they are moving in all axis, Y included), so I atached the DestroyByTime.cs to this bolts and now it's working, all dissapear from Hierarchy even if they scape from the boundary.
     
  12. dcolzani

    dcolzani

    Joined:
    Sep 22, 2014
    Posts:
    19
    Ok Adam thank you, one last question before I head over to the other forums. I'm testing my space shooter type game on Android and notice the boundary varies depending on the device. Any suggestions?
     
  13. opoisson

    opoisson

    Joined:
    Jan 8, 2016
    Posts:
    3
    I deployed it and it still didn't work.
     
  14. Zekovski

    Zekovski

    Joined:
    Jan 1, 2016
    Posts:
    1
    Goodmorning.

    I just ended up building my project in Android. It finally works well, but some of my textures became transparent. (the asteroids, player and ennemies)
    I've looked through the different build options, textures and materials properties, but as I'm a beginner in Unity I've probably missed something.

    Can someone help me please ?
     
  15. mobiletechie

    mobiletechie

    Joined:
    Jan 5, 2016
    Posts:
    2
    Hi.. My Bolt instances are not being destroyed by the Boundary. Is trigger is checked and from the scene view it looks like the boundary box surrounds the correct are in 3 dimensions.

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

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon,

    you need to Capitalise your function name, if you change it to have a capital O at the start it should be ok.
    Code (CSharp):
    1. void OnTriggerExit (Collider other)
    Bear in mind, C# is case SenSitive.
     
  17. mobiletechie

    mobiletechie

    Joined:
    Jan 5, 2016
    Posts:
    2
    Good grief! I can't tell you how many times I compared the code snippet with my version and didn't see that. Thanks.

    Out of interest, why wouldn't Visual Studio flag this sort of error? Or did it in some way I'm not aware of?
     
  18. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Sorry I cant answer that one, I dont know why, all i know is that the MonoBehaviours like Start() Update(), OnTriggerExit() etc have to have the correct case sensitivity to be called and work. They wont throw errors, they just wont work. something to be mindful of as theres no code completion or intellisense for these functions. so its just something I try to be careful about as a gotcha :)
     
    Last edited: Jan 17, 2016
  19. durlon

    durlon

    Joined:
    Jan 10, 2016
    Posts:
    2
    Hi, in tutorial Extending Space Shooter, in the Q&A you make 3 bolts shoot, but they are colliding and destroying each other, how would I make the bolts ignore themselves, but still have the OnTriggerEnter with the hazards and enemies?
     
  20. IronHatchett

    IronHatchett

    Joined:
    Jan 13, 2016
    Posts:
    1
    Im having trouble with my DestroyByTime. Its in the right spot and everything in the script is correct but the explosion clone for both the player and the asteroid just doesn't despawn. Any suggestions would be appreciated
     
  21. Deleted User

    Deleted User

    Guest

    From my understanding Application.LoadLevel is deprecated. That means it is no longer valid. From my understanding one is supposed to put at the top of each script using UnityEngine.SceneManagement; And then load the scene with something like:
    //calls the active scene
    string sceneName = SceneManager.GetActiveScene().name;

    //loads the active scene
    SceneManager.LoadScene(sceneName, LoadSceneMode.Single);

    you should also check that you reset the restart flag from false to true elsewhere in your scripts.

    On that note though I could use assistance myself because while all my objects reload... everything is a darker shade as if the lighting disappeared or is no longer working.
     
  22. Deleted User

    Deleted User

    Guest

    If by despawn you mean be destroyed afteer time I have the following attached to the prefab/vfxeffect [explosions] not the player object

    Code (CSharp):
    1. public class ScrObjectEndLifeCleanup : MonoBehaviour {
    2.  
    3.     public float objectLifetime;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.         Destroy(gameObject, objectLifetime);
    8.     }
    9. }
     
  23. Deleted User

    Deleted User

    Guest

    Regarding the reload lighting issue: It appears that the lighting issue on reload can be fixed by going to Window -> Lighting -> Scene tab -> adjust skybox and intensity settings. I derived that result from an old post that said to do the following:
    Window -> Lighting -> Lightmap Tab -> Disable Continuous Baking -> Press Build to bake the lighting once manually.
     
  24. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Ive just had a quick look again at the QnA section see what I can find.

    Its just an idea, but on your Bolt prefab, try and lock the Y position within the Rigidbody constraints.

    Its possible if you have done the same with the 3 bolt fire, rotating the shotspawn on each side.

    As soon as you bank the ship, this will change the forward direction of the shotspawn since the shotspawn local position is relative to the player, and therefore the travel direction of the bolt ( as it will inherit the forward vector of the shotspawn) would fire the bolt up or down slightly from the game plane (no longer at 0 on the Y axis), and this will put it out of the boundary and destroy it.

    try locking the Y axis see if that helps until Adam gets back to you.
     
    Last edited: Jan 17, 2016
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Thanks - I'm in meetings all week, but I'll try to wedge this in.

    If the hazards are colliding with each other and the isTrigger property is false, then the collisions will be true physics collisions and the hazards will knock each other into new paths - like balls on a billiards table. More importantly, if they don't have any rigidbody constraints set up, they could easily be knocked off the x-z plane and therefore, out of the game. If they keep their isTrigger property as true, then the should simply destroy each other on contact.

    What is the actual effect you are looking for? What do you want your hazards to do? Be aware that, in the current setup, the player cannot shoot any hazards coming up from below/behind.

     
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Am I correct in remembering that you've turned off isTrigger so these are standard physics objects? If so, then this is somewhat beyond the scope of this lesson. If you have a specific question, I'll try to answer it.
     
  27. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is also a somewhat complex issue... and is also related to resolution. One solution to look at this the "Hat Trick" game which is done in a set of 2 live training sessions. In that code, the play area is set up to be resolution independent, and uses the game camera to determine the bounds of the game. You could try to apply this to things like the hazard spawns and the boundary.
     
  28. plaisime5651

    plaisime5651

    Joined:
    Dec 4, 2015
    Posts:
    9
    I'm having a strange. I'm at the point of the project where I need to spawn the hazard, but for some reason when the asteroid is spawn is oesn't seem to want to move down.
     
  29. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have you attached the Mover script to the asteroid prefab and assigned a speed in the inspector?
    Any errors showing at all?
     
  30. plaisime5651

    plaisime5651

    Joined:
    Dec 4, 2015
    Posts:
    9
    No
     
  31. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    I have completed the first four lessons but when trying to make the space ship move I am getting compiler errors. I have read the updates and made them to no success.

    This is what i have now

    usingUnityEngine;
    usingSystem.Collections;

    publicclassnewplayer : MonoBehaviour
    {

    privateRigidbodyrb;

    voidStart ()
    {
    rb = GetComponent<Rigidbody>();
    }

    publicfloatspeed;

    {
    floatmoveHorizontal = Input.GetAxis ("Horizontal");
    floatmoveVertical = Input.GetAxis ("Vertical");

    rb.position = newVector3 (x, y, z);
    rb.velocity = movement * speed;

    }

    }


    Any lines I have wrong?

    Thank you
     
  32. DrDMoney

    DrDMoney

    Joined:
    Jan 19, 2016
    Posts:
    3
    I figured it out as I was having the same exact issue. I went through and tested just about everything.
    In all the VFX explosion prefabs I changed all of the particle setting of "Simulation Space" from world to Local and all errors and lag went away. I don't know why the setting is causing this lag.



     
  33. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    You say yor getting errors showing, what are they?

    When posting code, can u use code tags please, makes it easier to read.

    Have you added the FixedUpdate() function name , I cant see it in your listing.
     
  34. Zingbad

    Zingbad

    Joined:
    Dec 28, 2015
    Posts:
    2
    I'm having problems with ship movement, I thought I would use a different script put I cannot get it to work.
    This is the script.
    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    public float speed;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    float x = Input.GetAxisRaw("Horizontal");
    float y = Input.GetAxisRaw("Vertical");

    Vector2 direction = new Vector2(x, y).normalized;

    Move(direction);
    }

    void Move(Vector2 direction)
    {

    Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
    Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2 1, 1));

    max.x = max.x - 0.225f;
    min.x = min.x + 0.225f;

    max.y = max.y - 0.285f;
    min.y = min.y + 0.285f;

    Vector2 pos = transform.position;

    pos += direction * speed * Time.deltaTime;

    pos.x = Mathf.Clamp(pos.x, min.x, max.x);
    pos.y = Mathf.Clamp(pos.y, min.y, max.y);

    transform.position = pos;
    }
    }
    Can someone help please
    Thanks
     
  35. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Were you having trouble with the original script? would be best to try and iron out any problems with it and get it working initially and complete the course before testing out changes. you have a mix of 2D and 3D and swapping back and fore.

    as in 3d space the transform.position will give x, y, z and you are changing this to a Vector2 so only getting the x, y and not getting the z position data which the player moves on
     
  36. XxBeselXx

    XxBeselXx

    Joined:
    Jan 19, 2016
    Posts:
    10
    Hi everybody, i'm a starter programmer and i have a problem with the space shooter programing tutorial game. The Unity Engine has benn asking for me to put a RigidBody component on VFX Quad, but i already have settled it on the empty game object, which is a parent of the VFX Quad, just like the tutorial's teaching. I tried some different on the "Input" method, like: "Input.GetKey("up"), and it almost worked, but even so, when i push the "up" button tho shoot, the bolts don't go foward and i don't know what else i can try to solve this problem. Please, if anyone can help me, please, reply as faster as possible. the error showed on Unity is this one:

    Code (CSharp):
    1. MissingComponentException: There is no 'Rigidbody' attached to the "VFX" game object, but a script is trying to access it.
    2. You probably need to add a Rigidbody to the game object "VFX". Or your script needs to check if the component is attached before using it.
    3. UnityEngine.Rigidbody.set_velocity (Vector3 value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Physics/DynamicsBindings.gen.cs:1188)
    4. Mover.Start () (at Assets/Scripts/Mover.cs:8)

    Down bellow, there is the code i tried to use:



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

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    This is the error message

    Assets/Scripts/newplayer.cs(32,79): error CS0839: An argument is missing




     
  38. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    This is a screen capture of the script
     

    Attached Files:

  39. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning,

    Your screenshot of your script seems to differ from the code that you posted, based on the screenshot you need to capture and store the Rigidbody as you have done in your initial post using
    Code (CSharp):
    1. private Rigidbody rb;
    2.  
    3. void Start ()
    4. {
    5.        rb = GetComponent<Rigidbody>();
    6. }
    then whenever you need to reference and use the rigidbody of the player within the script, you need to use this stored reference, like
    Code (CSharp):
    1. rb.velocity = movement * speed;
    The error you are getting based on the line numbers is due to using the deprecaded rigidbody.velocity as this type of component access has been removed, so you have to find and store the reference to this like the code snippet above.

    have a go at that, should be good to go :)
     
  40. XxBeselXx

    XxBeselXx

    Joined:
    Jan 19, 2016
    Posts:
    10
    Guys, i'm glad for your help, but i made a check on my bolt scripting and there is no deprecaded script at all. I'll set the code down bellow, so you guys can make a second though about it:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mover : MonoBehaviour {
    5.  
    6.     public float speed;
    7.  
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.         GetComponent<Rigidbody> ().velocity = transform.forward*speed;
    12.     }
    13. }
    14.  
    This script is attached on VFX, a quad wich is used to input some texture. Also, it is attached on a empty GameObject called "Bolt", wich has the RigidBody component and it is also declared within the code i posted first. Just for safety and to be clare most of your doubts, i'll set the first script down bellow:
    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 float tilt;
    15.     public Boundary boundary;
    16.  
    17.     public GameObject shot;
    18.     public Transform shotSpawn;
    19.     public float fireRate;
    20.  
    21.     private float nextFire;
    22.  
    23.     void Update ()
    24.     {
    25.         if (Input.GetKey("up") && Time.time > nextFire)
    26.         {
    27.             nextFire = Time.time + fireRate;
    28.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    29.         }
    30.     }
    31.  
    32.     void FixedUpdate ()
    33.     {
    34.         float moveHorizontal = Input.GetAxis ("Horizontal");
    35.         float moveVertical = Input.GetAxis ("Vertical");
    36.  
    37.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    38.         GetComponent<Rigidbody>().velocity = movement * speed;
    39.  
    40.         GetComponent<Rigidbody>().position = new Vector3
    41.             (
    42.                 Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    43.                 0.0f,
    44.                 Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    45.             );
    46.  
    47.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    48.     }
    49. }
     
  41. OboShape

    OboShape

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

    There should be no scripts attached to the VFX quad, the mover script , rigidbody and collider should be on the Bolt parent only. have a check, but ensure that this is the same for your bolt Prefab as that is what is getting instantiated when you fire.

    Have another run through the Creating Shots video for more info
    http://unity3d.com/learn/tutorials/projects/space-shooter/creating-shots?playlist=17147
     
  42. DrDMoney

    DrDMoney

    Joined:
    Jan 19, 2016
    Posts:
    3
    Currently working with scoring and I have a bug I haven't been able to solve... I don't understand what I have done wrong.

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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour
    5. {
    6.     public GameObject explosion;
    7.     public GameObject playerExplosion;
    8.     public GameObject smallerAsteroid;
    9.     public int scoreValue;
    10.     private GameController gameController;
    11.  
    12.     public int asteroidCount;
    13.     public float size;
    14.     public float variance;
    15.  
    16.     void Start (){
    17.    
    18.         //Finds instance of gamecontoller object
    19.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    20.         if (gameControllerObject != null)
    21.         {
    22.             //gets the gamecontoller script
    23.             gameController = gameControllerObject.GetComponent <GameController>();
    24.         }
    25.  
    26.         if (gameController = null) {
    27.             Debug.Log("Cannot find 'GameController' script");
    28.         }
    29.  
    30.     }
    31.  
    32.  
    33.     void OnTriggerEnter (Collider other)
    34.     {
    35.         if (other.tag == "Boundry")
    36.         {
    37.             return;
    38.         }
    39.  
    40.         Instantiate(explosion, transform.position, transform.rotation);
    41.  
    42.         for (int i = 0; i < asteroidCount; i++)
    43.         {
    44.             smallerAsteroid.transform.localScale = new Vector3 (size,size,size);
    45.  
    46.             Instantiate (smallerAsteroid, new Vector3(transform.position.x + Random.Range(-variance,variance), 0.0f, transform.position.z + Random.Range(-variance,variance)), new Quaternion(Random.Range(-180,180),Random.Range(-180,180),Random.Range(-180,180),Random.Range(-180,180)));
    47.         }
    48.  
    49.         if (other.tag == "Player") {
    50.             Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    51.         }
    52.  
    53.  
    54.         gameController.AddScore(scoreValue);
    55.         Destroy (other.gameObject);
    56.         Destroy (gameObject);
    57.     }
    58.  
    59. }

    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 startWait;
    10.     public float spawnWait;
    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.         scoreText = gameObject.GetComponent<GUIText> ();
    22.     }
    23.  
    24.     //swawn hazzard
    25.     IEnumerator SpawnWaves()
    26.     {
    27.         yield return new WaitForSeconds (startWait);
    28.         while (true)
    29.         {
    30.             for (int i = 0; i < hazardCount; i++)
    31.             {
    32.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    33.                 Quaternion spawnRotation = Quaternion.identity;
    34.                 Instantiate (hazard, spawnPosition, spawnRotation);
    35.                 yield return new WaitForSeconds (spawnWait);
    36.             }
    37.         yield return new WaitForSeconds (waveWait);
    38.         }
    39.     }
    40.  
    41.     public void AddScore(int newScoreValue)
    42.     {
    43.         score += newScoreValue;
    44.         UpdateScore ();
    45.     }
    46.  
    47.     void UpdateScore(){
    48.         scoreText.text = "Score: " + score;
    49.     }
    50.        
    51. }
    52.  
     
  43. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836

    hehe, that one took me near an hour of staring to see, eventually had to put it in the scene to test, must be getting late :)

    but on line 26 of your DestroyByContact script, you are effectively setting the gameController to null with this line
    Code (CSharp):
    1. if (gameController = null) {
    change this to use the comparison operator
    Code (CSharp):
    1. if (gameController == null) {
    see how that goes.
     
  44. kicker10bog

    kicker10bog

    Joined:
    Jan 20, 2016
    Posts:
    3
    Hello, I just finished the Spawn Waves part of this tutorial and have a question. What could be causing mine to lag?

    Before the step where we remove explosions, my game was getting to down to 2 FPS. The game in the video seemed to do much better. Even after that step, my game still lags some and the FPS drops down to around 5 FPS.

    My computer is pretty new. It has an AMD FX 8320 8-core processor with 16 GB of RAM and a NVIDIA GeForce GTX 960 with 2 GB of on-board memory. So, I'm pretty sure my computer should be able to handle this game.

    What could be causing the the low FPS?

    Thank you
     
    Last edited: Jan 21, 2016
  45. DrDMoney

    DrDMoney

    Joined:
    Jan 19, 2016
    Posts:
    3
    You would think that this would through a compile error. This error was driving me crazy for about a hour before I posted it. Thanks for the help.

     
  46. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Just guessing I'm afraid, have a look to see if there's more than one rigid body on the asteroid or player, or duplicate components on any of the prefabs.

    Does it run ok if u comment out the spawning code, try dragging one asteroid into the scene during play mode and see what happens.
     
  47. kicker10bog

    kicker10bog

    Joined:
    Jan 20, 2016
    Posts:
    3
    I figured out the problem. I needed the first patch for 5.3.1. All it took was a little more searching.

    Thank you for your reply.
     
    OboShape likes this.
  48. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    nice one, good stuff :)
     
  49. XxBeselXx

    XxBeselXx

    Joined:
    Jan 19, 2016
    Posts:
    10
    Hi again
    i checked all of things you asked, and everything is on the right place, just like the video's teaching, but the bolts still's frozen on the field when i push the shot button and the error message still the same.
     
  50. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    As mentioned, the mover script should not be on the VFX quad, it should just be on the Bolt, the Mover script needs to be on the Parent Bolt object solely, as this is the object that has the rigidbody component that drives the shot.

    The VFX Quad should only have the rendering components and the transform, all the legwork is done on the parent bolt.

    If that's not the case, can u post a pic showing the inspector components for the bolt and also the vfx please.
     
    Last edited: Jan 21, 2016