- Home /
Random NullReferenceException
Hey guys,
I'm making a game that's based on touch. What I have is that every time the screen is touched, there are tiles on the screen that get activated. They are different types of tiles. I want that when a touch is sensed, ALL of the objects are activated at the same time. I have a couple of scripts controlling this like so:
First, TouchEntity.cs gameobjects are stored in an array so that they can be accessed when the screen is touched. When the script registers a touch, it uses a for loop to go through all the objects and activate touchBlock.OnTouched. (touchBlock is a c# script). touchBlock then uses a predetermined enum to activate a script based on the type of tile
Before I include my scripts, the problem is this: When I click play, everything runs fine. When I touch, the tiles activate like they should and work nicely. However, the console shows an error saying "NullReferenceException: Object reference not set to an instance of an object TouchEntity.Update () (at Assets/My Thingies/Scripts/TouchEntity.cs:26)" Even though I'll include the entire TouchEntity.cs script below, the line the error is referring to says:
touchActivatedObjects[i].GetComponent<touchBlock>().OnTouched();
What is weird is that even thought the error shows up every time I touch the screen, the game runs perfectly fine. I'm worried that this may actually be a problem that I'm simply just not seeing the effects of yet.
TouchEntity.cs: using UnityEngine; using System.Collections;
public class TouchEntity : MonoBehaviour {
[HideInInspector]
public GameObject[] touchActivatedObjects;
[HideInInspector]
public GameObject[] holdActivatedObjects ;
// Use this for initialization
void Start () {
touchActivatedObjects = GameObject.FindGameObjectsWithTag("touchActivated");
holdActivatedObjects = GameObject.FindGameObjectsWithTag("holdActivated");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
//Touch anywhere on the screen
//Later restrict certain areas of the screen such as the pause button so stuff doesn't start flying whent he player presses pause
for(var i = 0 ; i < touchActivatedObjects.Length ; i ++)
{
touchActivatedObjects[i].GetComponent<touchBlock>().OnTouched();
}
}
}
}
touchBlock.cs: using UnityEngine; using System.Collections;
public class touchBlock : MonoBehaviour {
private GameObject player;
public typeOfBlock type = typeOfBlock.undefined;
public enum typeOfBlock{
undefined,
pause,
jump,
extraJump,
turntableSpikes
}
void Awake () {
player = GameObject.FindWithTag("Player");
}
public void OnTouched () {
if (typeOfBlock.undefined == type) {
//Block doesn't have an assigner
Debug.LogError ("You need to assign the type of this block! Idiot.");
}
else if (typeOfBlock.pause == type) {
//It is a pause tile block
player.GetComponent<PlayerController>().speed = 2;
this.GetComponent<PauseBlock>().pausePlayer = false;
}
else if (typeOfBlock.jump == type) {
//It is a regular jump block
this.GetComponent<JumpBlock>().bounce();
}
else if (typeOfBlock.extraJump == type) {
//It is a double jump block
}
else if (typeOfBlock.turntableSpikes == type) {
//It is a turntable spike block
}
else {
//Block doesn't have an assigner
Debug.LogError ("I'm really confused cause by default you should be reaching case 0.");
}
}
}
Rather than calling GetComponent for each touchActivatedObject every time the mouse button is pressed, why not populate an array of all the touchBlocks in Start ()?
No worries it was solved by a stupid error of me having the script on objects that shouldn't have it :P Since this would be no help to anyone else could moderators please delete this question. thanks.
Your answer
Follow this Question
Related Questions
Increase value through frames, or in a while? 1 Answer
Determining groups in a 2D array by checking neighbours (and their neighbours etc) 2 Answers
How to create an image gallery with previous and next button with C#? 6 Answers
Problem with List not filling using add method in a foreach loop 1 Answer
Object reference not set to an instance of an object error C# 1 Answer