Forum begins after the advertisement:


[Part 6] Null Reference Exception on Knife Character and bug On Garlic Character

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 6] Null Reference Exception on Knife Character and bug On Garlic Character

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #13604
    Eleanor Co
    Participant

    I have some trouble on Part 6 when we spawning the starting weapon for each character:

    1. Whenever I selecting the knife character, there will be Null Reference Error.

    View post on imgur.com

    I assign the reference as the video below:

    View post on imgur.com

    And this is my code:
    KnifeController.cs:

    using UnityEngine;
    
    namespace Idol.Core.Weapons
    {
        public class KnifeController : WeaponController
        {
            // Start is called before the first frame update
            protected override void Start()
            {
                base.Start();
            }
    
            protected override void Attack()
            {
                base.Attack();
                GameObject spawnedKnife = Instantiate(weaponData.Prefab);
                spawnedKnife.transform.position = transform.position;
                spawnedKnife.GetComponent<KnifeBehaviour>().DirectionChecker(playerMovement.lastMovedVector);
            }
        }
    }

    And this is my WeaponController.cs:

    using Idol.Control.Character;
    using UnityEngine;
    
    namespace Idol.Core.Weapons
    {
        public class WeaponController : MonoBehaviour
        {
            [Header("Weapon Stats")]
            public Weapons weaponData;
            public GameObject prefab;
            float currentCooldown;
    
            protected PlayerMovement playerMovement;
    
            // Start is called before the first frame update
            protected virtual void Start()
            {
                playerMovement = FindObjectOfType<PlayerMovement>();
                currentCooldown = weaponData.CoolDownDuration;
            }
    
            // Update is called once per frame
            protected virtual void Update()
            {
                currentCooldown -= Time.deltaTime;
                if(currentCooldown <= 0f)
                {
                    Attack();
                }
            }
    
            protected virtual void Attack()
            {
                currentCooldown = weaponData.CoolDownDuration;
            }
        }
    }

    2. The Garlic doesn't show up whenever I choose the Garlic Character.
    This is the Garlic Controller's Inspector:

    View post on imgur.com

    This is the Garlic Scriptable Object's Inspector:

    View post on imgur.com

    and this is the Garlic's Prefab:

    View post on imgur.com

    As for the GarlicController.cs:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    namespace Idol.Core.Weapons
    {
        public class GarlicContorller : WeaponController
        {
            // Start is called before the first frame update
            protected override void Start()
            {
                base.Start();
            }
    
            protected override void Attack()
            {
                base.Attack();
                GameObject spawnedGarlic = Instantiate(weaponData.Prefab);
                spawnedGarlic.transform.position = transform.position;
                spawnedGarlic.transform.parent = transform;
            }
        }
    
    }

    I hope that you can help me for this problem. Thank you for your time!

    #13605
    Terence
    Keymaster

    Hi Eleanor, for the Knife, add the following and run the code. This will tell you on the Console which object is null in your script:

    using UnityEngine;
    
    namespace Idol.Core.Weapons
    {
        public class KnifeController : WeaponController
        {
            // Start is called before the first frame update
            protected override void Start()
            {
                base.Start();
            }
    
            protected override void Attack()
            {
                base.Attack();
                GameObject spawnedKnife = Instantiate(weaponData.Prefab);
                print(spawnedKnife);
                print(spawnedKnife.GetComponent<KnifeBehaviour>());
                spawnedKnife.transform.position = transform.position;
                spawnedKnife.GetComponent<KnifeBehaviour>().DirectionChecker(playerMovement.lastMovedVector);
            }
        }
    }

    For GarlicController, let’s add a print to see if the Attack() function is actually running, so we can see where we can start looking for propblems:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    namespace Idol.Core.Weapons
    {
        public class GarlicContorller : WeaponController
        {
            // Start is called before the first frame update
            protected override void Start()
            {
                base.Start();
            }
    
            protected override void Attack()
            {
                base.Attack();
                print("Garlic is attacking.");
                GameObject spawnedGarlic = Instantiate(weaponData.Prefab);
                spawnedGarlic.transform.position = transform.position;
                spawnedGarlic.transform.parent = transform;
            }
        }
    
    }
    #13606
    Eleanor Co
    Participant

    After trying the instruction, I found out that:
    1. Knife Controller failed to get the reference to KnifeBehaviour.
    2. The Attack() on GarlicController is running.

    I tried to re-watching the tutorial (part 3,4,6) and still can’t find the reason

    #13607
    Terence
    Keymaster

    Eleanor,

    For 1, double check if your assigned prefab has a KnifeBehaviour component attached. It is likely that the component is missing.

    For 2, can you add the line below and see what the output is? I have modified the code so that when the garlic attacks, the game will print the name of the Garlic object that is spawned and automatically pause. Try and find the GameObject on the Hierarchy, and see if it is hidden under the terrain.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    namespace Idol.Core.Weapons
    {
        public class GarlicContorller : WeaponController
        {
            // Start is called before the first frame update
            protected override void Start()
            {
                base.Start();
            }
    
            protected override void Attack()
            {
                base.Attack();
                print("Garlic is attacking.");
                GameObject spawnedGarlic = Instantiate(weaponData.Prefab);
                print(spawnedGarlic.name);
                Debug.Break();
                spawnedGarlic.transform.position = transform.position;
                spawnedGarlic.transform.parent = transform;
            }
        }
    
    }
    #13613
    Eleanor Co
    Participant

    Hello,

    1. I already attached the KnifeBehaviour into the Knife Prefab since the beginning with assigned value.

    View post on imgur.com

    2. I tried the code above and this is the result:
    a. The Garlic Controller spawned under the Player game object with Sorting layer on foreground Order in layer 1 meanhwile the background tile map spawned with sorting layer on background order in layar -2

    View post on imgur.com

    #13615
    Terence
    Keymaster

    Hi Eleanor,

    1. I already attached the KnifeBehaviour into the Knife Prefab since the beginning with assigned value.

    Can you check if you’ve assigned the correct prefab to the KnifeController then? There must be a reason why the null reference exception is occurring, and we have to find it if you want to fix this.

    2. I tried the code above and this is the result:
    a. The Garlic Controller spawned under the Player game object with Sorting layer on foreground Order in layer 1 meanhwile the background tile map spawned with sorting layer on background order in layar -2

    If you manually add the Garlic Controller prefab onto the player, does it show up? Alternatively, you can play the game, then pause it, and tinker with the Inspector to see what makes it show up.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: