简体   繁体   中英

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;

}
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM