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 ianbo · Jul 24, 2016 at 03:00 AM · listnullreferenceexceptionbooleanlistsinteger

Really weird behaviour with boolean variables from custom class

I have a class called Tile, which is composed of two integers and a boolean variable called isMine. In the following code, please focus your attention in the Debug.Log line and the if statement that follows.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 using System.Collections;
 using System;
 
 public class buttonController : MonoBehaviour
 {
     private float buttonX;
     private float buttonY;
     private List<Tile> tileList;
     private Button[] buttons;
     public Canvas boardHolder;
 
     public void OnTileClick()
     {
         buttonX = GetComponent<RectTransform>().localPosition.x / 30;
         buttonY = GetComponent<RectTransform>().localPosition.y / 30;
         buttons = GameObject.Find("Game Controller").GetComponent<gameController>().buttons;
         tileList = GameObject.Find("Game Controller").GetComponent<gameController>().tileList;
 
         Debug.Log("tileIsMine: " + tileList.Find(i => i.x == buttonX && i.y == buttonY).isMine);
 
         if (tileList.Find(i => i.x == buttonX && i.y == buttonY).isMine)
         {
             GameObject.Find("Game Controller").GetComponent<gameController>().GameOver();
         }
         else
         {
             GameObject.Find("Game Controller").GetComponent<gameController>().TileClick(gameObject);
         }
     }
 }

You see, whenever this is run, the Debug.Log line throws a NullReferenceException. Somehow, that tileList.Find is returning null.

Here's everything I have tried to check:

  1. Making sure buttonX and buttonY exist within tileList together.

  2. Making sure there are items in tileList that have isMine set to true AND false. While I haven't checked all of them, none of the times I ran the code did it not throw the exception, meaning it never found any of the trues and falses I found. It always returned null.

  3. Making sure this script locates tileList, and building tileList previous to usage of this script.

  4. Making a Tile and setting it to the tileList.Find code, and then using it.

By far the strangest thing is that this used to be working! It just suddenly stopped.

If you have any clue as to what may be causing this really strange behavior, please mention it to me. It could be anything I haven't mentioned.

Cheers!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ashleyjlive · Jul 24, 2016 at 03:28 AM

You need to initialize the tile list. Either make tileList public or change its declaration to private List<Tile> tileList = new List<Tile>();.

You may wonder why this is the case. Simply put, in C# classes must be 'created' - a list is a class. To create it the new keyword is used. However, public fields that can be serialized will automatically be created by Unity3D.

Comment
Add comment · Show 1 · 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 ianbo · Jul 24, 2016 at 04:45 AM 0
Share

In my other script called gameController (which is a component of an empty GameObject called "Game Controller") I did just that. You can see me setting the reference in line 20. Just so you know - and in case this helps you discern the issue further - I initialized it as public.

avatar image
0

Answer by Bunny83 · Jul 24, 2016 at 06:27 AM

Your buttonX and buttonY variables are float variables. So you're comparing float variables inside your lambda expression. Those comparisons will almost always fail due to floating point inaccuracy. You should declare your buttonX / Y variables as integers instead and round / floor / ceil the values the way you need.

Since it's still possible that Find might return null if the provided coordinates don't exist in the List, it's better to do a null check anyways:

 Tile tile = tileList.Find(i => i.x == buttonX && i.y == buttonY);
 if (tile != null)
 {
     Debug.Log("tileIsMine: " + tile.isMine);
     
     if (tile.isMine)
     {
         // [...]



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

53 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

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

How to initialize lists with set number of null elements? 1 Answer

how can I get values stored in a list? 3 Answers

Add Object to list error (JS). 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