Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Fearthefrozt · Oct 29, 2013 at 06:32 PM · c#nullreferenceexceptionfunction

NullReferenceException: Object reference not set to an instance of an object

This error is popping up as a result of me trying to call a function in one script from another I'm not sure what I'm doing wrong and I've tried a few different work arounds and none have worked for me. Here is the code I've written.

MainCameraAudio script using UnityEngine; using System.Collections;

 public class MainCameraAudio : MonoBehaviour {
         bool gameOverTest = false;
     public Instantiation script;
 
 void Awake(){
         gameOverSource = AddAudio(gameOver, false, false, 1);
 }
 
 void Start () {
     script = (Instantiation)this.GetComponent(typeof(Instantiation));
 }
 
 void Update () {
         if(script.CheckGameOver() && gameOverTest != true){
         audio.PlayOneShot(gameOverSource.clip);
         gameOverTest = true;
     }
 }

Don't worry about the AddAudio() function that's not the problem here. As well as the variable declarations, those are there I'm just not including them on here. The gameOverSource is an AudioSource with an audio clip attached to it. I'm trying to test if gameOver is true by accessing a script on this same object called Instantiation.

Instantiation script checkGameOver method

 public bool CheckGameOver(){
     if(gridEnvironment[playerCol, playerRow].tag == "Pit"){
         Debug.Log("GAME OVER YOU DEAD");
         dead = true;
     }
     return dead;
 }

I get the error and it says it's on the if statement in the method CheckGameOver() here. Am I accessing the script incorrectly or am I completely missing something. It's been bugging me for awhile now. Any help is appreciated, if you need to see more I'm willing to put the full scripts up but they are long and doubt that they would help at all, I'm fairly sure that the problem lies with the way that I'm calling the script. Thanks for your time.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kiloblargh · Oct 29, 2013 at 06:38 PM

The problem is probably your gridEnviroment. What is it an array of, are you sure it isn't empty; also, are playerCol and playerRow always defined before CheckGameOver() runs the first time?

Debug.Log messages will answer it for you, if you don't currently use Debug.Log, you need to start.

Comment
Add comment · Show 1 · 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 Fearthefrozt · Oct 29, 2013 at 07:23 PM 0
Share

Thank you very much the problem was that it wasn't setting the tag for the bare floors and so if would continue to give me a nullreferenceexception. Fixed it and now I don't get an error. $$anonymous$$uch appreciated.

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

16 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

Related Questions

C# Null Reference Exception in Custom Function 1 Answer

NullReferenceException help 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# nullreferenceexception object reference not set 1 Answer

NullReference when accessing GameObject in array (C#) 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