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 /
avatar image
0
Question by stevendale · May 11, 2016 at 10:32 AM · errornullreferenceexceptioncomponentnot working

Object reference not set to an instance of an Object C#

Hi guys! I need help with this thing that driving me nuts.

There are two scripts, one attached to ballPrefeb called "clickToDestroy.cs" and "activateAnimation.cs" that is attached to character who is going to be animated. The thing that I want is that when the ball is destroyed (clicked by OnMouseDown) it reads the position where has been destroyed and use that in "activateAnimation.cs" script for "triggering" particular animation based on that position. Problem (for now) is that I'm getting an error "NullReferenceException: Object reference not set to an instance of an object" in line 28. I assume that I'm getting this error because script doesn't know for what object are those positions. Please help me with this one! Here are the scripts.

This is "clickToDestroy.cs":

 using UnityEngine;
 using System.Collections;
 
 public class clickToDestroy : MonoBehaviour {
 
     public GameObject splashReference;
     public GameObject effectReference;
     public int scoreValue;
     private GameController gameController;
     public Vector3 _lastPosition;
 
   
 
     void Start()
     {
 
 
         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
         if (gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent<GameController>();
         }
         if (gameController == null)
         {
             Debug.Log("Cannot find 'GameController' script");
         }
     }
 
     public void OnMouseDown ()
     {
         gameController.AddScore(scoreValue);
         _lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
         Destroy(gameObject);
         Instantiate(splashReference, transform.position, transform.rotation);
         Instantiate(effectReference, transform.position, transform.rotation);
 
                 Debug.Log(_lastPosition);
 
     }
         }

And this is "activateAnimation.cs":

 using UnityEngine;
 using System.Collections;
 
 public class activateAnimation : MonoBehaviour {
 
     public Animator anim;
 
 
    public  clickToDestroy _clickToDestroy;
 
 
   //  GameObject yourObjects;
  //   private GameObject theCollision;
 
     public void Start()
     {
         anim = GetComponent<Animator>();
         _clickToDestroy = gameObject.GetComponent<clickToDestroy>();
 
 
     }
 
 
     public void Update()
 
     {
 
         if (_clickToDestroy._lastPosition.x >= 10 && _clickToDestroy._lastPosition.x <= 80)
             {
                 anim.SetTrigger("levi");
             }
         }
 
     }

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tyche10 · May 11, 2016 at 10:46 AM

As far as I can see both your scripts are attached to the ball that you are destroying. When a gameobject is destroyed, all scripts attached to it are destroyed as well. So if you want a script to outlive a gameobject you should attach it to another gameobject (for example an empty gameobject called Managers or something).

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
0

Answer by stevendale · May 11, 2016 at 11:04 AM

Thank you for your answer @Tyche10 !

I have GameController script and from there balls are getting generated as clones. So those clones are destroying script as well, even if i attached script to ballPrefeb? And if I try solution to make new gameobject, should I attach just "clickToDestroy.cs" to it?

Comment
Add comment · Show 12 · 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 Tyche10 · May 11, 2016 at 11:19 AM 0
Share

A monobehaviour script only works if there is an instance of it in the scene, in other words: if there is a gameobject in the scene that has the script attached to it, there is an instance of the script(component) in the scene. So you will only have an instance of the script attached to a prefab if there is an instance (or clone) of the prefab in the scene.

I would leave the clickToDestroy script on the ball, but move the _lastPosition variable to the gameController script. Then, before you make the ball destroy itself, update the _lastPosition value in the gameController script. The activateAnimation script should be attached to something other than the ball because you want it to still function after the ball is destroyed. The activateAnimation script then can get the necessary information about _lastPosition from the gameController script which also still exists after the ball has been destroyed (if it is attached to something other than the ball of course).

avatar image stevendale Tyche10 · May 11, 2016 at 11:35 AM 0
Share

Oh, thank you, I didn't know that about monobehaviour, I'm still new at this.

I like that idea to "save" _lastPosition variable in gameController script but could you please help me how to do it?

avatar image Tyche10 stevendale · May 11, 2016 at 11:48 AM 0
Share

Your gameController script should just declare a member variable:

 public class gameController: $$anonymous$$onoBehaviour {
 
 public Vector3 _lastPosition;
 
 ...
 }

And since you already keep a reference to the gameController in your clickToDestroy script, you can do:

 public void On$$anonymous$$ouseDown ()
          {
              gameController._lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
 
 Destroy(gameObject);
     }

ins$$anonymous$$d of

_lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);

Show more comments
avatar image stevendale Tyche10 · May 11, 2016 at 11:58 AM 0
Share

I wrote what you said and change in activateAnimation.cs:

   using UnityEngine;
     using System.Collections;
     
     public class AktiiranjeAnimacije : $$anonymous$$onoBehaviour {
     
         public Animator anim;
     
     
        public  GameController _clickToDestroy;
     
     
       //  GameObject yourObjects;
      //   private GameObject theCollision;
     
         public void Start()
         {
             anim = GetComponent<Animator>();
             _clickToDestroy = GetComponent<GameController>();
     
     
         }
     
     
         public void Update()
     
         {
     
             if (_clickToDestroy._lastPosition.x >= 10 && _clickToDestroy._lastPosition.x <= 80)
                 {
                     anim.SetTrigger("levi");
                 }
             }
     
         }
 

But I still getting "NullReferenceException: Object reference not set to an instance of an object" in line 28.

avatar image Tyche10 stevendale · May 11, 2016 at 12:09 PM 0
Share

First I would change the name of _clickToDestroy in your AktiiranjeAnimacije script to gameController for clarity, and make it private ins$$anonymous$$d of public so it doesn't interfere with your gameController variable in your _clickToDestroy script (I don't think they will necessarily interfere, since they can be recognized according to the class they're part of, but it's good practice to be sure). Give clear, unconfusing names to your variables and keep them as private as possible (also your functions are better off private if you don't need to access them from other scripts). Trust me, it will make things a lot easier for you down the road if you make that a habit ;)

Does he throw the null reference in line 28 in your activateAnimation script (is that script also still present?) or does he throw it in line 28 of your new AktiiranjeAnimacije script?

Show more comments
avatar image stevendale Tyche10 · May 11, 2016 at 01:46 PM 0
Share

First, I really appreciated your help, thank you! :)

It gives an error "_GameController is null. This is the problem"! And I've done thing for frame, I read something about that earlier but I'm so out of my head with this one. Also thank you for that help! :)

avatar image Tyche10 stevendale · May 11, 2016 at 02:13 PM 0
Share

No problem ;) So did you solve it now? Apparently the gameobject to which you attached AktiiranjeAnimacije script, doesn't have a GameController script attached to it, so when you call GetComponent(); it returns null. So all you should do is add a GameController script to the same object as the AktiiranjeAnimacije script

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

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

Related Questions

If statement not working correctly 1 Answer

UI null reference errors in script, but not in inspector. 1 Answer

I really need help with this message 0 Answers

Can't re-parent GameObject with Text(script) component. 0 Answers

I can't open my project 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