Search Unity

Merry Fragmas: Multiplayer FPS, part 1 "Playershooting" script issues

Discussion in 'Community Learning & Teaching' started by JanCDS, Apr 18, 2015.

  1. JanCDS

    JanCDS

    Joined:
    Jun 30, 2014
    Posts:
    15
    Hi,

    I've been following along with the Merry Fragmas tutorial, but I got stuck on an issue.
    ( http://unity3d.com/learn/tutorials/...raining-archive/merry-fragmas-multiplayer-fps )

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerShooting : MonoBehaviour {
    5.  
    6.     public ParticleSystem muzzleFlash;
    7.     Animator anim;
    8.     public GameObject impactPrefab;
    9.  
    10.     GameObject[] impacts;
    11.     int currentImpact = 0;
    12.     int maxImpacts = 5;
    13.  
    14.     bool shooting = false;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.  
    19.         impacts = new GameObject[maxImpacts];
    20.         for(int i = 0; i < maxImpacts; i++)
    21.             impacts[i] = (GameObject)Instantiate(impactPrefab);
    22.  
    23.         anim = GetComponentInChildren<Animator> ();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.         if(Input.GetButtonDown ("Fire1") && !Input.GetKey(KeyCode.LeftShift))
    30.         {
    31.             muzzleFlash.Play();
    32.             anim.SetTrigger("Fire");
    33.             shooting = true;
    34.         }
    35.  
    36.     }
    37.  
    38.     void FixedUpdate()
    39.     {
    40.         if(shooting)
    41.         {
    42.             shooting = false;
    43.  
    44.             RaycastHit hit;
    45.  
    46.             if(Physics.Raycast(transform.position, transform.forward, out hit, 50f))
    47.             {
    48.                 if(hit.transform.tag == "Enemy")
    49.                     Destroy (hit.transform.gameObject);
    50.  
    51.                 impacts[currentImpact].transform.position = hit.point;
    52.                 impacts[currentImpact].GetComponent<ParticleSystem>().Play();
    53.  
    54.                 if(++currentImpact >= maxImpacts)
    55.                     currentImpact = 0;
    56.             }
    57.         }
    58.     }
    59. }
    So that's the code, now at line 32, I get this error:
    "NullReferenceException: Object reference not set to an instance of an object
    PlayerShooting.Update () (at Assets/Scripts/PlayerShooting.cs:32)"

    I really don't have a clue as to why this is happening, as I've been following along exactly as in the tutorial. If anyone has a clue as to what could be causing this that would help a lot.

    If you need info on how the rest of the project is set up, I made no modifications to what is explained in the tutorial. (I was planning on doing that after I finished the tutorial, but first gotta get rid of those errors)
     
    Last edited: Apr 19, 2015
  2. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    It just sounds like the "anim = " call in your Start function is not finding an Animator component in the children.

    Not familiar with the tutorial you mentioned, but I'd check if your Animator is actually on the parent object and not a child.
    You can check easily by throwing a "Debug.Log(anim)" type line underneath your anim assignment line and see if it returns null in the console or not.

    Try removing the "InChildren" from your GetComponent call perhaps to check if it's on the parent instead.

    It's just something to check, without actually looking at the object setups, but it might be that simple.
    You'll just need to double-check where your Animator component is actually located.
     
    JanCDS likes this.
  3. JanCDS

    JanCDS

    Joined:
    Jun 30, 2014
    Posts:
    15
    Ah ok, many thanks. I'll try that as soon as I get the chance.

    Edit: After checking the console, the debug.log line did indeed return null, but removing the "InChildren" part from my GetComponent didn't help sadly. Might just be an issue with the script, as the tutorial is pre 5 I think, and I'm using 5 currently.

    (Also added a screenshot of my hierarchy, as you can see I'm certain that my animator component is indeed in one of the children. Still it returns null in the console though, with or without the "InChildren") Unity3D fragmas fps hierarchy.png

    Thanks for helping me out though, I'll tinker a bit more on my own, see if I can't get it to work.
     
    Last edited: Apr 23, 2015
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you're still stuck with the Merry Fragmas project, please post in the Live Training Thread and ask your questions there. The presenters often don't check the body of the forum, just the related official threads.
     
    JanCDS likes this.