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

Character Won't Shoot

Discussion in 'Scripting' started by zeroharpz, Apr 21, 2018.

  1. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    Hello, I've been following this tutorial almost exact, yet my character refuses to shoot...
    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerManager : MonoBehaviour {
    6.  
    7.     public GameObject BulletLeft, BulletLeft1;
    8.  
    9.     Transform firePos;
    10.  
    11.     public bool FacingRight;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         firePos = transform.FindChild("firePos");
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         if (Input.GetKeyDown(KeyCode.T))
    23.         {
    24.             Fire();
    25.         }
    26.  
    27.     }
    28.  
    29.     void Fire()
    30.     {
    31.         if (FacingRight)
    32.             Instantiate(BulletLeft1, firePos.position, Quaternion.identity);
    33.         if (FacingRight)
    34.             Instantiate(BulletLeft, firePos.position, Quaternion.identity);
    35.     }
    36.  
    37.  
    38. }
    39.  
     
  2. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    I would guess something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerManager : MonoBehaviour {
    5.     public GameObject BulletLeft, BulletLeft1;
    6.     Transform firePos;
    7.     public bool FacingRight;
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         firePos = transform.Find("ParentName/firePos");
    12.     }
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.T))
    17.         {
    18.             Fire();
    19.         }
    20.     }
    21.     void Fire()
    22.     {
    23.         if (FacingRight)
    24. {
    25.             Instantiate(BulletLeft1, firePos.position, Quaternion.identity);
    26.             Instantiate(BulletLeft, firePos.position, Quaternion.identity);
    27. }
    28.     }
    29. }
     
  3. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    Didn't seem to work... Character still won't shoot.
    The only thing I did different from the tutorial was make the shoot key be 'T' instead of the space bar; But, I'm not sure if that has anything to do with it.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    your first code snippet had 2 "if(FacingRight)" lines. Other than that, do you have any errors?
    If you Debug.Log in Fire() can you see a message in the console?

    Maybe the bullet is spawning in a weird spot, or the firepos wasn't found, or the bullet is destroyed right away? :)
     
  5. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    Okay, I am now noticing this weird error in Unity whenever I try to shoot that says:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerManager.Fire () (at Assets/PlayerManager.cs:32)
    PlayerManager.Update () (at Assets/PlayerManager.cs:24)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    There we go... So put some code/debug to check what on that line is null, then ensure it isn't null ;)

    Feel free to write back if you get stuck (or solve it..) :)
     
  7. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    Sorry, but it seems I'm really stumped. The problem was line 31-32 of my code that was causing the error, I deleted the code and I no longer get the error message. But still cannot shoot. I'm doing all the debugging I can and it just doesn't seem like anything is wrong with my script. However I will note that on the entirety of line 16 there is a green wavy line underneath. Though, MVS doesn't detect any errors with it. However, I don't think this is the problem...
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hmm.. If the line with the error matches what you posted, then BulletLeft1 or firepos must be null (that's the line, right)?
     
  9. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    It must be firePos, since I have an assigned object to BulletLeft1
    I'm not sure if this will help at all, but here is what my setup basically looks like
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, you can always check if it's null..
    you can also do this:
    Code (csharp):
    1. [SerializeField]
    2. Transform firePos;
    Drag n drop the transform in the inspector, and remove the line of code that tries to find it & try again :)
     
  11. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    How would I check if code is null in my case? I looked it up, and there seems to be a lot of different methods.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Even here would suffice..
    Code (csharp):
    1. void Start ()
    2. {
    3.   firePos = transform.FindChild("firePos");
    4.   if(firePos == null) print("Oops, didn't find it.");
    5. }
    Though I did just copy your code. 'FindChild' is actually deprecated, and you should use 'transform.Find'.. though for now it's safe, since I looked up the code and all it does is call that anyways. ;) It doesn't hurt to change deprecated code, though, when you see it.
     
  13. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    Didn't seem to do anything... Really sorry about this, I know this is starting to become difficult considering nothing seems to be working.
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol. Well, your code looked okay. Usually I avoid 'Find' like calls, but your transform tree is very small, so it's not a huge deal.

    Did you ever try the serialize field that I mentioned? Drag n drop the reference to see if that solved it?
    Just for fun do you want to test that & report back what happens in the game (or any errors, etc..)?

    If you're still stuck, feel free to post a Unity package in the thread with your character and whatever basic scripts are required to test this shooting. I can take a look at it when I have some free time. Shouldn't be too hard to track down what's wrong.
     
  15. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    When I put the serialize field code in it causes red underlines to appear under a lot of the code underneath it.
    (If I'm not following what you're saying correctly, it's because I'm very new at this)
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just this from my earlier post:
    Code (csharp):
    1.  
    2. [SerializeField]
    3. Transform firePos;
    So just the attribute is added above what you already had. Then drag n drop firePos from the hierarchy onto the spot in the inspector, which should show up.
     
    zeroharpz likes this.
  17. zeroharpz

    zeroharpz

    Joined:
    Apr 18, 2018
    Posts:
    12
    That did the trick! Thanks so much!
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, must have been a typo somehow then. I tried to verify by the screenshot, and it looked okay.
    Not sure, but glad it's working. :)

    This method is easier/more reliable I think anyways. Doesn't depend on you updating code if you change the name, for instance.

    Cheers - enjoy your game. =)
     
    zeroharpz likes this.