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 /
avatar image
0
Question by Catlard · Jun 02, 2011 at 01:15 AM · nullreferenceexceptionaccess

Null Reference Exception Error...specific error.

Hey folks! There's a problem with this script on the line I've indicated.

Just a simple coding error--with a really long, brute force script! I couldn't figure out how to put the scripts in an array, so I'm just doing this the hard way. But that's not the problem. The problem is that it's telling me there's a NullReferenceException, and that the Object Reference is not set to an instance of the object, and pointing to the first time I attempt to reference one of my scripts' Switch functions. All the scripts are the same, actually...just on eleven different objects. Print statements are telling me that the script that's giving me the error is Null...so I guess I'm not referencing any of this correctly?

I will tell tales of your heroism to all my friends in my design studio, if you can help me figure this one out. Thanks so much for your time. I feel stuck, right now...

Simon.

var maincamera:Camera; var eyeofbeholder : GameObject; var destroyer; destroyer = eyeofbeholder.GetComponent("Character Destroyer"); var wind_audio_object: GameObject; var backbeat_audio_object: GameObject; var banjo_audio_object: GameObject; var bass_audio_object: GameObject; var melody_audio_object: GameObject; var kick_and_hh_audio_object: GameObject; var tamborine_audio_object: GameObject; var snare_and_funk_audio_object: GameObject; var disco_hh_audio_object: GameObject; var mooooo_audio_object: GameObject; var boingy_audio_object: GameObject; var aloha_audio_object: GameObject;

var wind_audio_script; var backbeat_audio_script; var banjo_audio_script; var bass_audio_script; var melody_audio_script; var kick_and_hh_audio_script; var tamborine_audio_script; var snare_and_funk_audio_script; var disco_hh_audio_script; var mooooo_audio_script; var boingy_audio_script; var aloha_audio_script;

wind_audio_script = eyeofbeholder.GetComponent("Audio Switch"); backbeat_audio_script = eyeofbeholder.GetComponent("Audio Switch"); banjo_audio_script = eyeofbeholder.GetComponent("Audio Switch"); bass_audio_script = eyeofbeholder.GetComponent("Audio Switch"); melody_audio_script = eyeofbeholder.GetComponent("Audio Switch"); kick_and_hh_audio_script = eyeofbeholder.GetComponent("Audio Switch"); tamborine_audio_script = eyeofbeholder.GetComponent("Audio Switch"); snare_and_funk_audio_script = eyeofbeholder.GetComponent("Audio Switch"); disco_hh_audio_script = eyeofbeholder.GetComponent("Audio Switch"); mooooo_audio_script = eyeofbeholder.GetComponent("Audio Switch"); boingy_audio_script = eyeofbeholder.GetComponent("Audio Switch"); aloha_audio_script = eyeofbeholder.GetComponent("Audio Switch");

var flower:Transform; var bee:Transform; var cloud:Transform; var butterfly:Transform; var sign:Transform; var mesa:Transform; var mushroom:Transform; var tree:Transform; var cow:Transform; var rock:Transform; var rainbow:Transform;

var music_level = 0; var loop_length = 18; var number_of_objects = 0; var mybeat; var timecount = 0.00; var currentbeat = 0.00;

function Start() { StartLoop(); }

function StartLoop() { number_of_objects = destroyer.combocount; if(number_of_objects >= 1) { Invoke("Flower", .75); } if(number_of_objects >= 2) { Invoke("Bee", .75); } if(number_of_objects >= 3) { Invoke("Cloud", .75); } if(number_of_objects >= 4) { Invoke("Butterfly", .75); } if(number_of_objects >= 5) { Invoke("Sign", .75); } if(number_of_objects >= 6) { Invoke("Mesa", .75); } if(number_of_objects >= 7) { Invoke("Mushroom", .75); } if(number_of_objects >= 8) { Invoke("Tree", .75); } if(number_of_objects >= 9) { Invoke("Cow", .75); } if(number_of_objects >= 10) { Invoke("Rock", .75); } if(number_of_objects >= 11) { Invoke("Rainbow", .75); } if(music_level != number_of_objects || number_of_objects == 0) { switch(music_level) { case 0:

------------------> ERROR POINTS TO NEXT LINE <---------------- wind_audio_script.Switch();


break; case 1: backbeat_audio_script.Switch(); break; case 2: banjo_audio_script.Switch(); break; case 3: bass_audio_script.Switch(); break; case 4: melody_audio_script.Switch(); break; case 5: kick_and_hh_audio_script.Switch(); break; case 6: tamborine_audio_script.Switch(); break; case 7: snare_and_funk_audio_script.Switch(); break; case 8: disco_hh_audio_script.Switch(); break; case 9: mooooo_audio_script.Switch(); break; case 10: boingy_audio_script.Switch(); break; case 11: aloha_audio_script.Switch(); break; } music_level = number_of_objects; switch(music_level) { case 0: wind_audio_script.Switch(); break; case 1: backbeat_audio_script.Switch(); break; case 2: banjo_audio_script.Switch(); break; case 3: bass_audio_script.Switch(); break; case 4: melody_audio_script.Switch(); break; case 5: kick_and_hh_audio_script.Switch(); break; case 6: tamborine_audio_script.Switch(); break; case 7: snare_and_funk_audio_script.Switch(); break; case 8: disco_hh_audio_script.Switch(); break; case 9: mooooo_audio_script.Switch(); break; case 10: boingy_audio_script.Switch(); break; case 11: aloha_audio_script.Switch(); break; } } Invoke("StartLoop",loop_length); }

function Flower() { mybeat = Instantiate (flower, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Flower"; }

function Bee() { mybeat = Instantiate (bee, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Bee"; }

function Cloud() { mybeat = Instantiate (cloud, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Cloud"; }

function Butterfly() { mybeat = Instantiate (butterfly, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Butterfly"; }

function Sign() { mybeat = Instantiate (sign, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Sign"; }

function Mesa() { mybeat = Instantiate (mesa, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Mesa"; }

function Mushroom() { mybeat = Instantiate (mushroom, Vector3(-10, 1.7, transform.position.z+maincamera.farClipPlane), Quaternion.identity); mybeat.tag = "Mushroom"; }

function Tree() { mybeat = Instantiate (tree, Vector3(-10, 1.7, transform.position.z+maincamera.farClipPlane), Quaternion.identity); mybeat.tag = "Tree"; } function Cow() { mybeat = Instantiate (cow, Vector3(-10, 1.7, transform.position.z+maincamera.farClipPlane), Quaternion.identity); mybeat.tag = "Cow"; }

function Rock() { mybeat = Instantiate (rock, Vector3(10, 1.7, transform.position.z+maincamera.farClipPlane+1), Quaternion.identity); mybeat.tag = "Rock"; }

function Rainbow() { mybeat = Instantiate (rainbow, Vector3(10, 1.7, transform.position.z+maincamera.farClipPlane+1), Quaternion.identity); mybeat.tag = "Rainbow"; }

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 Cyb3rManiak · Jun 02, 2011 at 09:17 AM

This is way too much code to go over, but fortunately it's probably a pretty simple solution.

The part where you find the components:

 wind_audio_script = eyeofbeholder.GetComponent("Audio Switch");
 backbeat_audio_script = eyeofbeholder.GetComponent("Audio Switch");
 etc...

Is outside of any method. That area is for declaring variables, and unless you know what you're doing, you're not going to want to call any methods there. The behaviour will be unpredictable.

Simply put - you can do

 var wind_audio_script; // Since it's only a declaration
 or
 var myNumber = 5; // Since you're assigning a constant value


But you shouldn't do

 wind_audio_script = eyeofbeholder.GetComponent("Audio Switch");

Since GetComponent() is a method. Think about it - when exactly will it run? In the editor? When you press play? Before or after the Start() method?

What you should be doing, is calling all of these methods in your Start() or Awake() method. That's exactly what they are for.

So move all of your lines that use methods (in your case GetCompoenent()) like this:

 ...
 var wind_audio_object: GameObject;
 var backbeat_audio_object: GameObject;
 ...
 
 function Start()
 {
     Initialize();
     StartLoop();
 }
 
 function Initialize()
 {
     ...
     wind_audio_script = eyeofbeholder.GetComponent("Audio Switch");
     backbeat_audio_script = eyeofbeholder.GetComponent("Audio Switch");
     ...
 }
 ...

And as a side note - read some example scripts, and scripts posted by other people to structure your code better, and learn to use the tools the programing language gives you. I promise it will help you in the long run. No need to bruit force anything. You can't maintain scripts like this for a long time. Eventually they will become too big and complicated to handle.

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 Catlard · Jun 03, 2011 at 03:51 PM 0
Share

I really appreciate this. That's exactly what was wrong. And I only found out a day later...I wish I had done this the easy way. As far as learning not to brute force it, I think I just need to do what you're saying. Thanks so much!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to access a second script on same object 1 Answer

Accessing components through a collision 0 Answers

Need Help With a NullReference Exception 2 Answers

NullReferenceException 1 Answer

Null Reference Exception Hunt ! 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