user3682512
user3682512

Reputation: 23

get component error Unity3d C#

Here is my script that I keep getting null reference exception, but I don't know what to do? What do I assign where?

using UnityEngine;
using System.Collections;

public class CurrencyManagment : MonoBehaviour {

public int Coins;
public CreditPlayer CreditPlayerScript;

void Start ()
{
    CreditPlayerScript = GetComponent<CreditPlayer>();

}

void Awake()
{
    CreditPlayerScript = GetComponent<CreditPlayer>();

}

void Update ()
{
    Coins = CreditPlayerScript.CoinsTempContainer;

}
}

Upvotes: 0

Views: 176

Answers (1)

Roberto
Roberto

Reputation: 11953

It seems you don't have a CreditPlayer script attached to the same Game Object that you have a CurrencyManager. Simply drag and drop the script to the Game Object (with the game stopped, not running).

You can also do it programatically, by adding a RequireComponent attribute that will automatically add all scripts you specify. Take a look at this document.

Upvotes: 1

Related Questions