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. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Good Afternoon!

    I’m having difficult time trying to figure out how to make enemy and player bolt not destroy each other but pass through each other.

    Also, I notice that the boundary is not destroying all of the enemy’s bolts when they exit the boundary as the game progress. Some of them manage to slip through without being destroy. I added destroy by time to ensure any stubborn enemy bolts get destroy, yet I’m unsure if that the best way to solve it. Any explanation why it happening would be most appreciated.

    Edit: Alright, I did a bit of experimenting to test my theory why some of the enemy bolt manage to slip over the boundary. I would like to add that I had set enemy fire rate at random. When an enemy ship reaches to -5 on the x-axis near the boundary where all things get destroy onTriggerExit. If an enemy ship manages to fire on the -5 x-axis, the bolt will spawn on the other side of the boundary and into the void. Again, I’m uncertain if destroy by time is the best method to destroy any lucky enemy bolt that manage to escape the boundary of death. I’m zuper open to suggestions.
     
    Last edited: Dec 3, 2017
  2. unity_Ab56OhQt2R4Idg

    unity_Ab56OhQt2R4Idg

    Joined:
    Dec 1, 2017
    Posts:
    2
    Hello,

    I am new to Unity. I am now on my second tutorial (after finishing the Rolling Ball one) and I am having some difficulties on making the bolt firing.

    I am using this C# script on the Bolt prefab:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Mover : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.     public float speed;
    9.  
    10.     // Use this for initialization
    11.  
    12.     void Start () {
    13.         rb = GetComponent<Rigidbody>();
    14.  
    15.         rb.velocity = transform.forward * speed;
    16.     }
    17.  
    18. }
    What I find curios is that when I instanciate the Bolt prefab on the Scene, the bolt moves on the Y axis, instead of the Z axis. I have managed to work around this by specifing the bolt to move precisely on the Z axis using:

    Code (CSharp):
    1. rb.velocity = new Vector3(0f, 0f, 1f) * speed;
    It works perfectly now. I have added a Rigibody on my prefab and Use Gravity is NOT checked.

    What I also find curios is something on my Capsule Collider Compnent:
    The direction is set on the Y-Axis and it looks like on the Direction.png file

    When setting the Direction on the Z-Axis as specified in the tutorial it will end up looking like on the Direction2.png file.

    Am I doing something wrong? Even if I managed to correct it by using Vector3, I still wish to know what is wrong here. Thanks in advance. :)
     

    Attached Files:

  3. JxBalance

    JxBalance

    Joined:
    Dec 4, 2017
    Posts:
    3
    need help...
    i'm creating shots as the video told me, and there is no error with my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Mover : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private Rigidbody rb;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         rb=GetComponent<Rigidbody>();
    14.         rb.velocity= new Vector3(0.0f, 0.0f, 1f) * speed;
    15.     }
    16. }
    but whenever i drag in the shot, it stuck on the screen.

    could someone find whats wrong with it?
     
  4. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    @unity_Ab56OhQt2R4Idg
    I'm curious, have you figure out your problem yet?

    @JxBalance
    Did you set your speed in the inspector window. It might be set at 0.
     
  5. unity_Ab56OhQt2R4Idg

    unity_Ab56OhQt2R4Idg

    Joined:
    Dec 1, 2017
    Posts:
    2
     
  6. GorkemP

    GorkemP

    Joined:
    Aug 29, 2013
    Posts:
    1
    Hi,

    When creating the shooter why do we need the 'shot spawn' GameObject exactly?

    Bolt prefab has already includes transform component, we can get the position and rotation from bolt prefab itself, why the following codes are not sufficient?

    Code (CSharp):
    1. Instantiate(shot);
    or

    Code (CSharp):
    1. Instantiate(shot, shot.transform.position, shot.transform.rotation);
    When these codes are used, when fire is pressed prefabs are created and they are moved, it can be seen in the hierarchy window.

    Yet, it is not visible in the Game or Scene window. Why it is not visible?

    On the other hand, in play mode, if I just drag and drop prefab into hierarchy, it is created and visible in the Scene and Game windows.

    If answer to this question is that prefab's own transform component can not be used because the prefab's itself is not instantiated, why it works when we drag and drop prefab into the hierarchy? where does it takes its position and rotation?
     
    Last edited: Dec 5, 2017
  7. JxBalance

    JxBalance

    Joined:
    Dec 4, 2017
    Posts:
    3
    it is twenty:confused: i change it in to 20000000, the shot is still stuck
     
  8. JxBalance

    JxBalance

    Joined:
    Dec 4, 2017
    Posts:
    3
    ooooooooh I get it!!!
    i added the rigidbody to the VFX rather than the Bolt, so it doesn't work:)
     
    PajamaJohn likes this.
  9. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    @JxBalance
    Great work on figuring that one out!


    @GorkemP

    The prefab of bolt takes its position and rotation from transform in your inspector window, which is probably set to 0,0,0. As for your script, it will always instantiate Bolt position and rotation which is probably set to 0,0,0.

    Edit
    : When you move Bolt prefab into the hierarchy, did you disable Player Ship? There is a chance Bolt is at the same origin with Player Ship, 0,0,0; therefore it appear to be inside the Player Ship. Try disabling Player Ship or change Bolt position.
     
    Last edited: Dec 7, 2017
  10. arnhmail

    arnhmail

    Joined:
    Oct 14, 2017
    Posts:
    13
    Thank you, I figured it out as follows:

    turns out my randomrotator.cs and asteroidmover.cs scripts were locking each other up when both were trying to modify the same rigidbody from two different scripts, so I compiled them into one script and it seems to be running well.

    Don't know if that explains it, but by taking the same code and copying it into one file, it fixed the problem.
     
  11. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    I have resolved my issue with enemy and player bolt destroying each other on contact. While I was digging around for more information, I have come across layer. This have produce the desirable result I been looking for. For those of you who are curious, create a layer to hold both your Enemy Bolt and Player bolt. Then go into Edit > Project Setting > Physic, on Layer Collision Matrix, unchecked layer that is assign to Enemy Bolt and Player Bolt. This will allow anything under Bolt layer not to collide with each other. Although I’m still curious how would I accomplish this in code.
     
    Last edited: Dec 9, 2017
  12. BlackKnife12

    BlackKnife12

    Joined:
    Dec 8, 2017
    Posts:
    2
    Hi there,Adam

    I'm currently working on the Space Shooter game and I'm nearly done with the project.
    However,I'm having issues with the GUIText. I've followed the tutorials but the texts ('Score','Restart', and 'Game Over') don't appear. I even have the the Transform positions between 0 and 1,they still don't show up on my screen.
     
  13. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
  14. p4hv1

    p4hv1

    Joined:
    Dec 12, 2017
    Posts:
    1
    Not sure has anyone else had this but I have two asteroids spawning simultaneuosly and exploding each other.
     
  15. HC_Zhang

    HC_Zhang

    Joined:
    Nov 18, 2017
    Posts:
    6
    Here are what i found in unity manual and Scripting API:
    Transform.forward: The blue axis of the transform in world space.
    Direction :The axis of the capsule’s lengthwise orientation in the object’s local space.
    Maybe you have changed the rotation of the bolt and the blue axis is now the Y axis in world space .This change also results in the second problem.
    I am new to unity too,i hope this is helpful.
     
  16. HC_Zhang

    HC_Zhang

    Joined:
    Nov 18, 2017
    Posts:
    6
    Hi,guys..
    I have done my Space Shooter game but i find something interesting.

    When i finish the enemy ship object and drag it to the Hazards array of the game controller object,hit the play button and it works fine.But the enemy ship doesn't move forward at all if you directly drag the enemy ship to the hierachy and play it .it seems the Mover script doesn't work in the latter situation.

    In order to figure out this ,i compare the mover script and the EvasiveManeuver script and come up with a hypothesis.First let 's take a glance at these two scripts.
    Mover:
    Code (CSharp):
    1. public class Mover : MonoBehaviour
    2. {
    3.     private Rigidbody rb;
    4.     public float speed;
    5.     void Start()
    6.     {
    7.         rb = GetComponent<Rigidbody> ();
    8.         rb.velocity = transform.forward * speed;
    9.         Debug.Log ("velocity set!");
    10.     }
    11. }
    EvasiveManeuver:
    Code (CSharp):
    1. void Start ()
    2.     {
    3.         rb = GetComponent<Rigidbody> ();
    4.         currentSpeed = rb.velocity.z;
    5.         Debug.Log ("currentSpeed set!");
    6.         StartCoroutine (Evade ());
    7.  
    8.     }
    9.    
    10.     IEnumerator Evade()
    11.     {
    12.         yield return new WaitForSeconds (Random.Range (startWait.x,startWait.y));
    13.         while (true)
    14.         {
    15.             targetManeuver = Random.Range (1,dodge)*-Mathf.Sign(transform.position.x);
    16.             yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y));
    17.             targetManeuver = 0;
    18.             yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y));
    19.         }
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         float newManeuver = Mathf.MoveTowards (rb.velocity.x, targetManeuver, Time.deltaTime * smoothing);
    25.         rb.velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
    26.         rb.position = new Vector3
    27.             (
    28.             Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    29.             0.0f,
    30.             Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    31.         );
    32.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    33.     }
    As you can see ,the final velocity.z of enemy ship is determined by the value of currentSpeed in EvasiveManeuver script.the currentSpeed is assigned to rb.velocity.z in the Start function.We set rb.velocity.z to a specific value in the mover script.

    The hypothesis is that 'currentSpeed = rb.velocity.z;' sentence executes before 'rb.velocity = transform.forward * speed;' sentence in the latter case,so the currentSpeed will always be zero and the enemy ship can't move forward.

    To verify this i added two debug sentences in the mover script and EvasiveManeuver script respectively and the output in console proved the guess.

    Anyone knows what makes the defference?
    Thanks in advance.
     
  17. lorenfitts

    lorenfitts

    Joined:
    Nov 20, 2017
    Posts:
    1
    I have not seen an answer on this and I need it figured out. being on the unity website and so outdated, they need to just create a new Tutorial for Space Shooter.

    When I write in my script for ship movements via the "w,a,s,d" keys, I get no reaction from my ship when I play the game and the error is this:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:22)

    my Script:
    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<Rigidbody>();
    Rigidbody rigidbody = new Rigidbody();
    Rigidbody.velocity = movement;
    }

    }
     
  18. Bluesky_93

    Bluesky_93

    Joined:
    Dec 14, 2017
    Posts:
    3
    I think my UNITY Engine is broke^^

    It all worked really really well until the video 14.

    I checked now everything for at least 6hours.
    I compared my script to the "Done" scripts - everything is perfect but it does not work.

    Even when I try to play the "Done_Main" game - it does not work.
    The console replys with compiling errors (see the screenshot).
    Thats so frustrating... if someone finds the error - please tell me.

    Funny thing is: my game does work - but now the asteroids are missing. they do not spawn anymore.
    Also sometimes Unity reads my "GameController1.cs" script and sometimes it doesnt (I do not change anything)
    When it doesnt, I says "the associated script can not be loaded." wich ist total BS, because 2 minutes earlier it did.
    Also - when I play the game, the "Score Text" stays as a place holder.
     

    Attached Files:

    Last edited: Dec 14, 2017
  19. Doni16

    Doni16

    Joined:
    Dec 11, 2017
    Posts:
    2
    I recently started Space Shooter Tutorial with Unity 2017.2 and I have problem regarding player explosion. I wrote my code exactly as shown in the tutorial and everything is happening in the same way but when I hit the asteroid with my player they don't explode; just the player gives a strong force to asteroid and asteroid is moving as a result. What can be the reason?
     
  20. Bluesky_93

    Bluesky_93

    Joined:
    Dec 14, 2017
    Posts:
    3

    I agree, they have to make a new one - but your issue was posted in the YT commentary section about 20times and all the top comments are full with it^^ AND they also wrote the solution as an overlay into the video. xD
    I'm not judging but...yeah.

    you need to get your Rigidbody in void Start.


    private Rigidbody rb;

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

    .
    .
    .

    rb.velocity = movement;
     
  21. Bluesky_93

    Bluesky_93

    Joined:
    Dec 14, 2017
    Posts:
    3
    You need to post a screenshot. It's not possible to see the issue otherwise. :)
     
  22. Doni16

    Doni16

    Joined:
    Dec 11, 2017
    Posts:
    2
    Here is are the details:
    DestroyByForce:

    using System.Collections;
    using UnityEngine;

    public class DestroyByContact : MonoBehaviour
    {
    public GameObject explosion;
    public GameObject playerExplosion;

    void OnTriggerEnter(Collider other)
    {
    if (other.tag == "Boundary")
    {
    return;
    }
    Instantiate (explosion, transform.position, transform.rotation);
    if (other.tag == "Player")
    {
    Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    }
    Destroy(other.gameObject);
    Destroy(gameObject);
    }
    }

    Player Controller:

    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;
    public float tilt;

    public GameObject shot;
    public Transform shotSpawn;
    public float myTime = 0.0F;
    public float fireDelta = 0.5F;

    private float nextFire = 0.5F;

    void Update ()
    {
    myTime = myTime + Time.deltaTime;

    if (Input.GetButton ("Fire1") && myTime > nextFire)
    {
    nextFire = myTime + fireDelta;
    Instantiate (shot, shotSpawn.position, shotSpawn.rotation);

    nextFire = nextFire - myTime;
    myTime = 0.0F;
    }
    }

    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)
    );

    GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    }
    }
     

    Attached Files:

  23. unity_P_E6_wAQcwWw2g

    unity_P_E6_wAQcwWw2g

    Joined:
    Dec 15, 2017
    Posts:
    2
    Hi I have a problem when play the game, if i shot to asteroid, the game crash.

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

    C# Script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameController : MonoBehaviour {
    public GameObject hazard;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;
    public GUIText scoreText;
    private int score;

    // Use this for initialization
    void Start ()
    {
    score = 0;
    UpdateScore ();
    StartCoroutine (SpawnWaves ());
    }

    IEnumerator SpawnWaves ()
    {
    yield return new WaitForSeconds (startWait);
    while (true)
    {
    for (int i = 0; i < hazardCount; i++) {
    Vector3 spawPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    Quaternion spawnRotation = Quaternion.identity;
    Instantiate (hazard, spawPosition, spawnRotation);
    yield return new WaitForSeconds (spawnWait);
    }
    yield return new WaitForSeconds (waveWait);
    }
    }

    public void AddScore (int newScoreValue)
    {
    score += newScoreValue;
    UpdateScore();
    }

    void UpdateScore()
    {
    scoreText.text = "Score: " + score;
    }
    }
     

    Attached Files:

  24. JGrace

    JGrace

    Joined:
    Nov 25, 2017
    Posts:
    1
    Hi I'm new, I've made the Space Shooter up to adding the explosions. I get no errors but no explosions are appearing. Here's my script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class DestroybyContact : MonoBehaviour
    {
    public GameObject explosion;
    public GameObject playerExplosion;
    void OnTriggerEnter(Collider other)
    {
    if (other.tag == "Boundary")
    {
    return;
    }
    Instantiate(explosion, transform.position, transform.rotation);
    if (other.tag == "Player")
    {
    Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    }
    Destroy (other.gameObject);
    Destroy (gameObject);
    }
    }

    There's a pencil next to the DestroybyContact script...does that matter? It says 'Default references will only be applied in edit mode"
    upload_2017-12-17_11-39-40.png

    I've also tried unticking Play on Awake* in the asteroid VFX inspector as that was mentioned in another Q&A...to no avail
     
  25. thesixx

    thesixx

    Joined:
    Dec 17, 2017
    Posts:
    1
    Hey guys!

    im working on the spaceshooter project but:
    for some reason my spaceship goes down on the Z value and wont slide left or right and shoot once its builden till the end with no problems but suddenly this came up and i can't find the error!
     
  26. peteowl

    peteowl

    Joined:
    Dec 16, 2017
    Posts:
    3
    I'm not there yet in the tutorial, I'll reply later if I have the same issue when I get there (I'm new to Unity but I'm a seasoned coder so I can still answer some not-too-unity-related questions).

    These two scripts are different, so there will be an order of execution. In your case it seems that the Start() for the mover gets executed before the Start() for the Evasive maneuver.
    To solve this I suggest you put the code for getting the current speed in your EvasiveManeuver.FixedUpdate code, it will make more sense I think, to get the current speed at the moment of the evasive maneuver and not at the object spawn. [Edit]: I just did that part and actually got rid of currentSpeed entirely, I'm just using rb.velocity.z directly - my suggestion still stands, it will have the same effect.

    The error suggest that one of the references in your script is not set, you need to make sure all your references are properly set.
    Check at line 35 to see which reference seems to be unset.
    Attaching to Unity, bebugging and using a breakpoint might help (Visual Studio can do it, not sure about the built in editor).

    @All, a general remark: please use the CODE tags when posting code, it makes it much easier if anyone wants to help.

    Cheers - P
     
    Last edited: Dec 18, 2017
  27. peteowl

    peteowl

    Joined:
    Dec 16, 2017
    Posts:
    3
    I think the message with the pencil indicates you did some changes in play mode and these changes will be lost when exiting play mode. Only changes done in edit mode are saved.
    If the script is correct with respect to the tutorial video, check the references, you can review your project to verify if the references are set or maybe put some debug code at the beginning of the trigger:
    Code (CSharp):
    1.  
    2. if (explosion == null)
    3.     Debug.Log("explosion is NOT set!");
    4. else
    5.     Debug.Log("explosion is set");
    6. if (playerExplosion == null)
    7.     Debug.Log("playerExplosion is NOT set!");
    8. else
    9.     Debug.Log("playerExplosion is set");
    10.  
     
  28. peteowl

    peteowl

    Joined:
    Dec 16, 2017
    Posts:
    3
    It looks like your collider is not a trigger collider. Try changing the asteroid's collider in the inspector, check "Is Trigger"
     
  29. HC_Zhang

    HC_Zhang

    Joined:
    Nov 18, 2017
    Posts:
    6
    Thanks for your reply.
    It works fine by using your suggestion.But the point is what causes the difference(dragging it to the scene directly it doesn't move forward but dragging it to the hazards array it can move forward).In other words what determines the execution order of the same fuction(like Start()) in different scripts of a GameObject?
    Thanks again!
     
  30. bertenernie

    bertenernie

    Joined:
    Dec 16, 2017
    Posts:
    7
    I got stuck on Ending the game , where it said to add :
    Application.LoadLevel (Application.loadedLevel);
    i was getting errors, i got past it doing this :
    SceneManager.LoadScene("Main");
    is this the right use ? and now i continued in extending, im confused what to do with the vortex/vertex? mapping, doesnt seem to work for me, where can i read up on that, Hold V and ? Thnx in advance 23 min in video
     
  31. HC_Zhang

    HC_Zhang

    Joined:
    Nov 18, 2017
    Posts:
    6
    1.Hold V
    2.Move your cursor on one point
    3.Drag it to another point
     

    Attached Files:

    • 1.png
      1.png
      File size:
      262.4 KB
      Views:
      918
    • 2.png
      2.png
      File size:
      262.6 KB
      Views:
      867
    • 3.png
      3.png
      File size:
      278.9 KB
      Views:
      904
    bertenernie likes this.
  32. mike_leask

    mike_leask

    Joined:
    Aug 4, 2017
    Posts:
    1
    Hi Adam, Your tutorial has been great. I was wondering if wouldn't mind an addition to the PlayerController script. I have been trying to modify the script to use an Xbox controller with the RightTrigger. I understand the right trigger is an axis and Input.GetAxis will need to be used instead of Input.GetButton but I am running into errors. How could the code be modified to use the RightTrigger?
    [Solved]
    In the Edit-->Project Settings-->Input, I created (renamed an existing input) the XB1_RightTrigger to use the video game controller, and set Type: Joystick Axis, and Axis: 10th Axis (Joysticks), Gravity: 0, Dead: 0.1, Sensitivity:1.
    In C#, I did not declare a local float value for the RightTrigger (I'm still brand new to C# and Unity) and used your code to fire but changed it to include the GetAxis("RightTrigger") and added !=, thus:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetAxis("XB1_RightTrigger") !=0 && Time.time > nextFire)
    4.         {
    5.             nextFire = Time.time + fireRate;
    6.             Instantiate(singleShot, shotSpawn.position, shotSpawn.rotation);
    7.         }
    8.     }
     
    Last edited: Dec 21, 2017
  33. pwaugh

    pwaugh

    Joined:
    Dec 25, 2017
    Posts:
    6
    Hello,

    On page 14 of the of the Space Shooter Upgrade Guide to Unity 5, I believe there is an error.

    It shows the following code:

    Code (CSharp):
    1.  
    2. private Rigidbody;    // no property name, just the type
    3.  
    4. void Start () {
    5.     rb = GetComponent<Rigidbody>();  // rb is of undefined type
    6. }
    7.  
    when it should be:

    Code (CSharp):
    1.  
    2. private Rigidbody rb;    // added the name of the local private property
    3.  
    4. void Start () {
    5.     rb = GetComponent<Rigidbody>();
    6. }
    7.  
     
  34. natev

    natev

    Joined:
    Nov 17, 2017
    Posts:
    1
    I've worked around some issues* and am ready to build.

    As per instructions in this thread, I've set my build platform to WebGL. When I build, it asks for a folder; when I provide one, it takes some time building, but when it's done, the folder remains empty. Build + run doesn't do anything. Building to a PC target gives me a playable executable.

    Any idea what I should do to continue this tut? I'd love to get some experience building for web.

    Edit: using Unity 2017.3.0f3, with UnitySetup-WebGL-Support-for-Editor-2017.2.0f3.

    Edit2: Discovered error messages on console:
    "
    ArgumentException: Could not map typename 'NScreenBridge' to type info (WebGL class registration skipped classes)
    UnityEditor.WebGL.WebGlBuildPostprocessor.FindTypeByNameChecked (System.String name) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:300)
    UnityEditor.WebGL.WebGlBuildPostprocessor.ModifyIl2CppOutputDirBeforeCompile (System.String outputDir) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:317)
    UnityEditorInternal.IL2CPPBuilder.Run () (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/Il2Cpp/IL2CPPUtils.cs:158)
    UnityEditorInternal.IL2CPPUtils.RunIl2Cpp (System.String stagingAreaData, IIl2CppPlatformProvider platformProvider, System.Action`1 modifyOutputBeforeCompile, UnityEditor.RuntimeClassRegistry runtimeClassRegistry, Boolean debugBuild) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/Il2Cpp/IL2CPPUtils.cs:41)
    UnityEditor.WebGL.WebGlBuildPostprocessor.CompileBuild (BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:340)
    UnityEditor.WebGL.WebGlBuildPostprocessor.PostProcess (BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:893)
    UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTargetGroup targetGroup, BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.BuildReporting.BuildReport report) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:272)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)"

    Unfortunately, since I don't believe I've messed with any of that, not sure what the issue is. Maybe because I've been ignoring the warnings that Application.loadedLevel and Application.loadLevel() are obsolete?


    *Most recently, not addressed as far as I can tell in this thread, issues with GUI Text not showing up, hints from Youtube commenter:

    Menno Gouw 2 years ago
    As of Unity 4.6 GUI text is deprecated. I just added a UI.Text item to my scene hierarchy (Canvas text). But then you cannot drag that into the "public" slot you create in the GameController. All you have to do is change "public GUIText scoreText;" into "public UnityEngine.UI.Text scoreText". You can find this by going to the help of the canvas Text element and look for the namespace it is in.
     
    Last edited: Dec 28, 2017
  35. RorschachBrennt

    RorschachBrennt

    Joined:
    Dec 28, 2017
    Posts:
    1
    I got the latest version of Unity 2017.3 and I wanna ask if WebGL is the same as Web Player in the tutorial and what do I do when it asks me for "Open Download Page"
     
  36. pwaugh

    pwaugh

    Joined:
    Dec 25, 2017
    Posts:
    6
    Yes, you use WebGL.

    Not sure on the "Open Download Page" part of your question. If you mean to download the assets, they are here:

    Download the assets for free from the Asset Store here.
     
  37. KeinZantezuken

    KeinZantezuken

    Joined:
    Mar 28, 2017
    Posts:
    53
    Yeah, I've noticed the same issue. Seems like something has changed in collider logic in Unit y2017 and it is having issues handling too many projectiles exiting it's field.
     
  38. pwaugh

    pwaugh

    Joined:
    Dec 25, 2017
    Posts:
    6
    The reason the programmer chose to create the shotSpawn object has to do with Object Oriented Design.

    You want to “program to an interface”, not a “concrete instance”.

    When you think about it, you may want to attach your prefab bolt to many different Objects, such as Player, or Enemy, or even many as yet undeveloped enemies. Each of these will likely have a different point at which you want to spawn that bolt or even multiple bolts.

    So, rather than having to modify every bolt, you instead have the Objects model (the ship) define where a bolt will spawn. Thus, the bolt needs no knowledge of what is attached to in order to work.

    This is a concept known as encapsulation.

    Hope this helps.
     
    Last edited: Jan 2, 2018
  39. voidpresence

    voidpresence

    Joined:
    Jan 3, 2018
    Posts:
    1
    On 'Extending Space Shooter", I've gotten to the point where the enemy ship shoots back, but upon game start, the enemy ship explodes on its first shot. I've seen a previous post with the same issue but I have not found a response to that one. I've checked tags and the DestroyOnContact script and cannot find the cause.
     
  40. pwaugh

    pwaugh

    Joined:
    Dec 25, 2017
    Posts:
    6
    My guess would be that either your enemy is being destroyed upon the spawn of a bolt it shoots (DestroyOnContact), or that it is immediately coming into contact with your Boundary due to it not being properly located or the Boundary being the right size. Turn off the DestroyOnContact script, then run and see if this "fixes" the problem. If so, then there's where to look, and you can post the script.
     
  41. Legolad22

    Legolad22

    Joined:
    Sep 30, 2017
    Posts:
    1
    my shots are not shooting, i have double checked my code and cannot find any errors

    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.     public float speed;
    14.     public Boundary boundary;
    15.     public float Tilt;
    16.  
    17.     public GameObject shot;
    18.     public Transform shotSpawn;
    19.     public float fireRate;
    20.  
    21.     private float nextFire;
    22.  
    23.     void update ()
    24.  
    25.  
    26.     {
    27.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    28.         {
    29.             nextFire = Time.time + fireRate;
    30.             GameObject clone =
    31.                 Instantiate(shot, shotSpawn.position, shotSpawn.rotation);// as GameObject;
    32.  
    33.         }
    34.     }
    35.  
    36.     private Rigidbody rb;
    37.     void Start ()
    38.     {
    39.         rb = GetComponent<Rigidbody> ();
    40.     }
    41.     void FixedUpdate ()
    42.     {
    43.         float moveHorizontal = Input.GetAxis ("Horizontal");
    44.         float moveVertical = Input.GetAxis ("Vertical");
    45.  
    46.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    47.         rb.velocity = movement * speed;
    48.  
    49.         rb.position = new Vector3 (
    50.             Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    51.             0.0f,
    52.             Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    53.         );
    54.            
    55.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -Tilt);
    56.  
    57.     }
    58. }
    59.  
     
  42. KeinZantezuken

    KeinZantezuken

    Joined:
    Mar 28, 2017
    Posts:
    53
    Update() case sensitive
     
  43. mpmlopes

    mpmlopes

    Joined:
    Jan 5, 2018
    Posts:
    5
    Hi,

    I'm following the tutorial, set it up as WebGL, I've changed the resolution to 600x900, but the view is still not portrait. Am I missing something?

    upload_2018-1-5_21-50-40.png

    UPDATE: Turns out I just needed to change "Free Aspect" in the top left of the Game window to "Standalone (600x900).
     
    Last edited: Jan 5, 2018
  44. Jeomar

    Jeomar

    Joined:
    Jun 30, 2017
    Posts:
    4
    Can't seem to get my spawn hazards script to work. Not sure why, I've copied the exactly as Adam has it in the video. I checked to make sure that I had the connection to the asteroid in Unity. any help would be greatly appreciated.

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


    4. public class GameManager : MonoBehaviour
    5. {
    6. #region Public Variables
    7. public GameObject hazard;
    8. public Vector3 spawnValues;
    9. #endregion

    10. void Start()
    11. {

    12. }

    13. void SpawnWaves()
    14. {
    15. Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    16. Quaternion spawnRotation = Quaternion.identity;

    17. hazard = Instantiate(hazard, spawnPosition, spawnRotation) as GameObject;
    18. }
    19. }
     
  45. Jeomar

    Jeomar

    Joined:
    Jun 30, 2017
    Posts:
    4
    Never mind, I think I understand why it's not working. I forgot to make a method call in start!
     
  46. aerito

    aerito

    Joined:
    Jan 7, 2018
    Posts:
    2
    hi .. i am currently trying to upgrade this project ..so i wanna know how can i give a life bar to player ship ?
    i tried to insert another script but does not work? any suggestion...
     
  47. pwaugh

    pwaugh

    Joined:
    Dec 25, 2017
    Posts:
    6
    You gave us no information on how you planned to implement this new feature, or what you have tried. To get an answer try asking a very specific question.
     
  48. BBaSSa

    BBaSSa

    Joined:
    Jan 8, 2018
    Posts:
    1
    too late to reply
    i'm also beginner but i found out my mistake
    so want to check out whether you have a mistake like me
    my mistake was a ' lifeTime ' in DestroyByTime.cs
    in the video, he applied this script to 2 prefabs at once
    but i loose my concentration so i applied this script to only 1 prefab, concretely 'explosion_asteroid'

    so you check out prefab named ' explosion_player ' and lifetime in it
    if it is zero, you can change it to another number more than 0

    hope you guys my reply is useful
     
    Last edited: Jan 8, 2018
  49. aerito

    aerito

    Joined:
    Jan 7, 2018
    Posts:
    2
    i want to add boss fight and make some drop objects after we blast of some asteroids like extra life and shields for the player..help plz..
     
  50. mpmlopes

    mpmlopes

    Joined:
    Jan 5, 2018
    Posts:
    5
    For those having problems with text not showing up, here's how I've done it, to get the text to work:

    1. I've created a new GameObject of type Canvas, by doing Create->Ui->Canvas on the Hierarchy window
    2. I've created a new GameObject of type Text, by doing Create->Ui->Text, and dragged it into the Canvas
    3. On the GameController.cs file, I've imported UnityEngine.UI by doing: using UnityEngine.UI;
    4. I've declared a public property in the GameController class of type Text: public Text scoreText;
    5. In Unity, I've selected the GameController GameObject, and now in the script component there's a box called Score Text. I've dragged the ScoreText into that box.

    Everything else stays the same as in the tutorial, except for how you position the text on the screen.