I am getting the following error NullReferenceException: Object reference not set to an instance of an object
Coin.Start () (at Assets/Coin.js:4)
This is the two scripts:
Coin.js #pragma strict
var Money : MenuShopSystem;
function Start () {
Money = GameObject.Find(“Shop”).GetComponent(MenuShopSystem);
}
using UnityEngine;
public class Coin_Generate : MonoBehaviour
{
public GameObject Coin;
// Use this for initialization
void Start()
{
Debug.Log ("Generate coin");
InvokeRepeating("CreateObstacle", 1f, 1.5f);
}
void CreateObstacle()
{
Instantiate(Coin);
}
}
The second script is cloning the coin and the first is telling the coin what to do
If no GameObject “Shop” can be found, then you’ll be trying to call GetComponent on a null object. Instead, check that GameObject.Find actually returns something that’s valid, then try GetComponent.
As a side note, I’m not too sure what you’re trying to achieve with that statement as later on, in your trigger function you already have a reference to the MenuShopSystem so you’ve either already got the reference or you’ll run into another null error because it isn’t set and you’re trying to access the .Money property.
As phil_me_up said, you are trying to access GameObject “Shop” and it’s component “MenuShopSystem” but looks that either of them is not found.
Try modifying your Start() function like that
function Start()
{
if (GameObject.Find("Shop") == null)
{
Debug.Log("No Shop found!")
}
else if (GameObject.Find("Shop").GetComponent(MenuShopSystem) == null)
{
Debug.Log("No MenuShopSystem component found in Shop GameObject")
}
else
{
Money = GameObject.Find("Shop").GetComponent(MenuShopSystem);
Debug.Log("Shop found with its component MenuShopSystem!");
}
}
I’m not really familiar with JavaScript, but that should give you some clue what’s the problem.
Search Google for RequireComponent too.
Also, it’s not recommendable to use JavaScript and C# scripts together like that, but that seems not to be the problem.