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 Lumu Joshua · Apr 08, 2011 at 09:34 AM · collidernullreferenceexception

ControllerColliderHit Vairable object Instance Exception in c#;

Hello Gamer

I am finding problems with my On controllerColliderHit "hit" that am using to check if the ball object has collided with either the player or the terrain/ground Am using c# and this is my code

it throws a NullReferenceException: Object instance not set to an instance of an object

please help!!!!

using UnityEngine;

using System.Collections;

using System.Timers;

public class BallScript : MonoBehaviour {

public Texture2D chance0;

public Texture2D chance1;

public Texture2D chance2;

public Texture2D chance3;

public Texture2D chance4;

public Texture2D chance5;

private GameObject g_chances;

private const int NumOfBalls =10;

private int bounds;

private int chance=6;

public static int scoreMade=0;

private int force;

private ControllerColliderHit hit;

public static bool isCollided=false;

// Use this for initialization

void Start () {

hit=new ControllerColliderHit();

}

// Update is called once per frame

void Update () {

 if (hit.collider.gameObject.tag == "Player") {

     chance = chance - 1;

 }

 if (hit.collider.gameObject.tag == "ground") {

     scoreMade = scoreMade + 1;

 }



         if (chance == 0)

         {

             if (bounds >= 3)

             {

                 Application.LoadLevelAdditive(2);

             }

             else

             {

                 Application.LoadLevelAdditive(4);



             }







             chanceChange();

             isCollided = false;

             if (scoreMade >= NumOfBalls)

             {

                 bounds = scoreMade / NumOfBalls;



             }

         }

}

void OnControllerColliderHit(ControllerColliderHit hit)

{

 if (hit.gameObject.tag=="player")

 {

     DestroyImmediate(gameObject);

     isCollided = true;

 }

 if(hit.gameObject.tag == "ground") {

     Destroy(gameObject);



     GameObject.Find("ScoreText").guiText.text = scoreMade.ToString();

 }



}

public void chanceChange() {

 g_chances = GameObject.Find("C006");



 if (chance == 5)

 {

     g_chances.guiTexture.texture = chance5;

 }

 if (chance == 4)

 {

     g_chances.guiTexture.texture = chance4;

 }

 if (chance == 3)

 {

     g_chances.guiTexture.texture = chance3;

 }

 if (chance == 2)

 {

     g_chances.guiTexture.texture = chance2;

 }

 if (chance == 1)

 {

     g_chances.guiTexture.texture = chance1;

 }

 if (chance == 0)

 {

     g_chances.guiTexture.texture = chance0;

 }

}

public int scoreAccess

{

 get{return scoreMade; }

}

public int BoundsAcess

{

 get{ return bounds; }



}

}

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 Ray-Pendergraph · Apr 08, 2011 at 10:43 AM 0
Share

Your code did not post correctly. Also, when referencing exceptions it would be helpful if you pointed out which line the exception occurred since we can't tell real line numbers here. The stack trace is your best friend in this instance. It will tell you which line is bad. There are lots of things in this post that can be null

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Meltdown · Apr 08, 2011 at 09:44 AM

Go through each object in your script and make sure it is not null.

I'd suggest using Debug.Log on your chanceChange method to make sure the object 'C006' you are looking for is actually being found, and that it has all the relevant textures assigned to it in the inspector.

Also make sure any public properties on your script are assigned to at design time in the inspector.

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
Wiki

Answer by Lumu Joshua · Apr 08, 2011 at 10:59 AM

Okey Just A Little clearence the reception is thrown at hit.gameObject.tag=="Player" in the update function

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

No one has followed this question yet.

Related Questions

Raycast causes game to crash when shooting up 1 Answer

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

NullReferenceException by trying to access another script. 0 Answers

How to null-reference check a Collider2D that's been destroyed 1 Answer

NullReferenceException question. 2 Answers


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