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
- This topic has 5 replies, 2 voices, and was last updated 8 months ago by Terence.
-
AuthorPosts
-
March 21, 2024 at 11:56 pm #13604::
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.
I assign the reference as the video below:
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:This is the Garlic Scriptable Object's Inspector:
and this is the Garlic's Prefab:
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!
March 22, 2024 at 3:29 am #13605::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; } } }
March 22, 2024 at 6:39 am #13606::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
March 22, 2024 at 7:35 pm #13607::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; } } }
March 23, 2024 at 3:25 pm #13613::Hello,
1. I already attached the KnifeBehaviour into the Knife Prefab since the beginning with assigned value.
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 -2March 24, 2024 at 4:15 pm #13615::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 -2If 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.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: