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 aman_jha · Jul 16, 2014 at 07:04 PM · c#errorarraytouchloop

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.");
         }
     }
 
 }
 
Comment
Add comment · Show 2
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 tanoshimi · Jul 16, 2014 at 07:26 PM 0
Share

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 ()?

avatar image aman_jha · Jul 16, 2014 at 10:09 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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