Question is off-topic or not relevant.
NullReferenceException: Object reference not set to an instance of an object
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); }
function OnTriggerEnter2D (other: Collider2D)
{
if(other.gameObject.tag == "Player")
Debug.Log ("Touch");
MenuShopSystem.Money += 100 ;
Destroy(this.gameObject);
}
Coin Generate.cs
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
NullReferenceExceptions are anwered many times and asked hundreds of times.
There are 4 problems:
Your question can be answered by using search on the forum or even google.
Your question does not contain a question.
Your code is not formatted properly.
Don't post NullReferenceException questions to Unity Answers. It is not for them. You most likely need to get more knowledge on scripting if you run into (null) references and can't solve them on your own. That's not a bad thing, it's just something a beginner might have problems with. Check out the scripting tutorials, they are amazingly packed with knowledge and easy to understand. Since they are just Youtube videos, you can watch them as much as you need to. :D
If you have a solution, regardless who found it, close the question so the forum stays as organized as possible. It is hard enough for dedicated moderators to maintain a certain degree of discipline and order here, so why make it even harder?
Please read the forum guidelines (FAQ and User Guide) again to avoid posting non-relevant questions.
PS: Please don't take this as a personal attack against you. Some people in the past seemed to take even the slightest criticism personally and start arguments. I'm just pointing out that the forum has certain rules. I hope you can understand that. ;)
Answer by ShadyProductions · Jan 02, 2016 at 02:18 PM
the script doesn't make sense
#pragma strict
var mss : MenuShopSystem;
function Start () {
if (GameObject.Find("Shop") != null && GameObject.Find("Shop").GetComponent<MenuShopSystem>() != null)
mss = GameObject.Find("Shop").GetComponent<MenuShopSystem>();
}
function OnTriggerEnter2D (other: Collider2D)
{
if(other.gameObject.tag == "Player")
Debug.Log ("Touch");
mss.Money += 100 ;
Destroy(this.gameObject);
}
try this?
Answer by phil_me_up · Dec 23, 2015 at 01:16 AM
You're not checking if your objects actually exist and your references are correct.
For example, here:
GameObject.Find("Shop").GetComponent(MenuShopSystem)
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.
ok i think i get what you are saying but the coin script works fine and has no errors when being used by its self but as soon as it is being cloned by coin_generate then it comes up with the errors and the $$anonymous$$oney variable also works fine. But i dont now what is causing the gameobject "Shop" not to be found by the script becuase as i said before it finds the gameobject "Shop". Do you know why?
maybe you should try the advice above first?
if(GameObject.Find("Shop") != null)
$$anonymous$$oney = GameObject.Find("Shop").GetComponent($$anonymous$$enuShopSystem);
But what do you need the reference for? you are already accessing it here
$$anonymous$$enuShopSystem.$$anonymous$$oney += 100 ;
every time the coin is destroyed it adds 100 to the variable from an other script. The script works fine on its own but when cloned the whole thing does not work
The money variable is from the shop script and i am accessing it using the coin script
#pragma strict
var $$anonymous$$oney : $$anonymous$$enuShopSystem;
function Start () {
$$anonymous$$oney = GameObject.Find("Shop").GetComponent($$anonymous$$enuShopSystem);
}
function OnTriggerEnter2D (other: Collider2D)
{
if(other.gameObject.tag == "Player")
Debug.Log ("Touch");
$$anonymous$$enuShopSystem.$$anonymous$$oney += 100 ;
Destroy(this.gameObject);
}
Also i am using a prefab storing the coin script
Answer by $$anonymous$$ · Jan 03, 2016 at 09:02 AM
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.
Follow this Question
Related Questions
Error) DataReader already active on this command 0 Answers
Unable to join player connection alternative multicast group. 1 Answer
Array index is out of range. 1 Answer
*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer
Not sure what this error is telling/asking me to do? 1 Answer