- Home /
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; }
}
}
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
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.
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
Your answer
Follow this Question
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