Having issues not Adding value or taking away

if anyone is familiar with this section i dont know what i am doing wrong. I have followed as instructed any ideas or have i just looked at my screen too long?

Enemy script:
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Enemy : MonoBehaviour

{

[SerializeField] int goldReward = 25;

[SerializeField] int goldPenalty = 25;

Bank bank;

void Start()

{

bank = GetComponent<Bank>();

}

public void RewardGold()

{

if (bank == null) { return; }

bank.Deposit(goldReward);

}

public void StealGold()

{

if (bank == null) { return; }

bank.Withdraw(goldPenalty);

}

}

EnemyHealth Script:
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Enemy : MonoBehaviour

{

[SerializeField] int goldReward = 25;

[SerializeField] int goldPenalty = 25;

Bank bank;

void Start()

{

bank = GetComponent<Bank>();

}

public void RewardGold()

{

if (bank == null) { return; }

bank.Deposit(goldReward);

}

public void StealGold()

{

if (bank == null) { return; }

bank.Withdraw(goldPenalty);

}

}


Hi Andre,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hope this helps. :slight_smile:


See also:

Your code shows enemy.StealGold() at position 63 (where the error is happening). That means there is no enemy. The problem is you have typed Start with a lowercase ‘s’. It doesn’t get executed, so your enemy is never set to anything

1 Like

thanks i changed it yes. I debug log came out Null meaning something wrong with enemy script.

Thank you so much i knew it was going to be something stuiped

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms