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 Amy · Feb 21, 2010 at 01:47 AM · nullreferenceexception

Anyone Else wanna take a wack at the question ? Need help fixing a script error...

Okay

"NullRefereceException: Object reference not st to an instance of an object UnityEngine.UIStyle.Draw(Rect position, Boolean isHover, Boolean on, Boolean hasKeyboardFocus)"

appears when I press play. here is the script. An object " the battery" is suppose to be destroyed, make a sound when it does and then a GUI texture of how many batteries were collected is suppose to appear.

This is the script that goes on the FPC -

var batteryCollect : AudioClip;

private var doorIsOpen : boolean = false; private var doorTimer : float = 0.0; private var currentDoor : GameObject;

var doorOpenTime : float = 3.0; var doorOpenSound : AudioClip; var doorShutSound : AudioClip;

function Update() { var hit : RaycastHit; if(Physics.Raycast (transform.position,transform.forward, hit, 5)) { if(hit.collider.gameObject.tag == "outpostDooor" && doorIsOpen == false) { currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor); } } if(doorIsOpen){ doorTimer += Time.deltaTime;
if(doorTimer > doorOpenTime){ Door(doorShutSound, false, "doorshut", currentDoor); doorTimer = 0.0; } } }

function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "outpostDoor" && doorIsOpen == false){ currentDoor = hit.gameObject;
Door (doorOpenSound, true, "dooropen", currentDoor);
} }

function OpenDoor() { audio.PlayOneShot(doorOpenSound); doorIsOpen = true; var myOutpost : GameObject = GameObject.Find("outpost");
myOutpost.animation.Play("dooropen"); }

function shutDoor() { audio.PlayOneShot (doorShutSound);
doorIsOpen = false; var myOutpost : GameObject = GameObject.Find ("outpost");
myOutpost.animation.Play("doorshut");
}

function Door(aClip : AudioClip, openCheck : boolean, animName : String, thisDoor : GameObject) { audio.PlayOneShot(aClip); doorIsOpen = openCheck; thisDoor.transform.parent.animation.Play(animName); }

function OnTriggerEnter(collisionInfo : Collider) { if(collisionInfo.gameObject.tag == "battery") { BatteryCollect.charge++; audio.PlayOneShot(batteryCollect); Destroy(collisionInfo.gameObject); }

 @script RequireComponent(AudioSource);

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

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jaap Kreijkamp · Feb 21, 2010 at 02:04 AM

It's not easy to give an answer without knowing in which line the exception occurs, the exception should tell you in which file and line number unity was when things got wrong. The null pointer exception means that you're trying to access an object from a variable, but the variable is not set.

In your case this could happen if there isn't an object outpost. That would make:

var myOutpost : GameObject = GameObject.Find("outpost");

result in myOutpost being NULL (not holding a reference to an object). The line

myOutpost.animation.Play ("doorshut");

would give a null pointer exception as you can't get the animation component if myObject doesn't point to an object.

Comment
Add comment · Show 6 · 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 Amy · Feb 21, 2010 at 02:15 AM 0
Share

well it says the problem is at line : -1 which didn't make any sense to me. The other 2 problems u listed are working as they are suppose to. Also its also saying "expecting }, found '@script'. Just for some reason the batteries are not destroy after the FPC is moving over them...and no sound. :(

avatar image Amy · Feb 21, 2010 at 02:32 AM 0
Share

Basically everything working except for this part

function OnTriggerEnter (collisionInfo : Collider) {

if(collisionInfo.gameObject.tag == "battery") {

BatteryCollect.charge++;audio.PlayOneShot (batteryCollect);Destroy(collisionInfo.gameObject);}

@script RequireComponent (AudioSource) ;

avatar image Sebas · Feb 21, 2010 at 03:12 AM 0
Share

In your above posted code you're missing one } at the very end. You didn't close the { of your OnTriggerEnter function.

avatar image Sebas · Feb 21, 2010 at 03:13 AM 0
Share

To clarify: Thats's before your @script RequireComponent (AudioSource) ;

avatar image Amy · Feb 21, 2010 at 03:25 AM 0
Share

Okay now its saying excpecting EOF, found ;

Show more comments
avatar image
0

Answer by RobF · Feb 21, 2010 at 07:56 AM

look at you last function - missing a }

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 felivans · Nov 20, 2010 at 03:35 AM

on trigger enter doesent suport "collideinfo" it must be (other : Collider) i think

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 Loius · Nov 20, 2010 at 03:42 AM

The error very much sounds like one of your GUI calls is used incorrectly.

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 schaddemm · Jan 31, 2013 at 10:06 AM

Try commenting:

//Destroy(collisionInfo.gameObject);

From what I see this ones your best shot at causing a null reference error if you've got everything assigned correctly in the editor.

You could be causing a null reference by destroying some object tagged "battery"(also check that you haven't tagged something wrong) and destroying some component that lives upon it that some other script wants to access.

Other thing you should do is put parts of your code in comments and see what you need to take away before it starts working again, so that you isolate your problem better.

Other good thing to do is after you hit play you hit pause and go in the editor over the objects and see if theres a missing component somewhere.

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

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

1 Person is following this question.

avatar image

Related Questions

NullPointerException while changing a variable from one script in another 1 Answer

array initialisation c# 1 Answer

Unity the NullReferenceException wizard 2 Answers

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

NullReferenceException 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