Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 02, 2016 at 03:42 PM by Le-Pampelmuse for the following reason:

Question is off-topic or not relevant.

avatar image
-1
Question by Blade666 · Dec 22, 2015 at 09:03 AM · c#javascripterrorobjectinstance

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · Jan 02, 2016 at 03:42 PM 2
Share

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. ;)

3 Replies

  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blade666 · Dec 23, 2015 at 08:49 AM 0
Share

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?

avatar image ShadyProductions Blade666 · Jan 02, 2016 at 01:33 PM 0
Share

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 ;
avatar image Blade666 ShadyProductions · Jan 02, 2016 at 01:48 PM 0
Share

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

Show more comments
avatar image Blade666 Blade666 · Jan 02, 2016 at 02:11 PM 0
Share

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);
  
  }
avatar image Blade666 Blade666 · Jan 02, 2016 at 02:18 PM 0
Share

Also i am using a prefab storing the coin script

avatar image Blade666 Blade666 · Jan 02, 2016 at 02:24 PM 0
Share

Thank you soooo much (rep given)

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges