Author Topic: Atlas returning NullReference Exception  (Read 3960 times)

kwelts

  • Guest
Atlas returning NullReference Exception
« on: July 25, 2012, 01:32:43 PM »
I've made 2 atlases in the same manner and one works perfectly but the other keeps on getting NullReference.

Here's the code:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UIButtonTexture : MonoBehaviour {
  5.        
  6.         public UIAtlas ReferenceAtlas;
  7.     public GameObject ReplacementAtlas;
  8.         public string HomePanelTopBG;
  9.     public string TopNavBar;
  10.        
  11.         bool AtlasSwitch;
  12.         /*public UIButtonTexture(bool AtlasSwitch) {
  13.         this.AtlasSwitch = AtlasSwitch;
  14.         } */
  15.        
  16.         public enum Trigger {
  17.                 OnClick,
  18.         }
  19.        
  20.         public Trigger trigger = Trigger.OnClick;
  21.        
  22.        
  23.         void Start() {
  24.         AtlasSwitch = false;
  25.         }
  26.        
  27.         public void OnClick() {
  28.                 Debug.Log("---> Inside OnClick");
  29.                 if (AtlasSwitch == false) {
  30.                         AtlasSwitch = true;
  31.                         Debug.Log("---> AtlasSwitch = "+AtlasSwitch);
  32.                                 UpdateAtlas();
  33.                         } else {
  34.                         AtlasSwitch = false;
  35.                         Debug.Log("---> AtlasSwitch = "+AtlasSwitch);
  36.                                 UpdateAtlas();
  37.                         }
  38.         }
  39.        
  40.         public void UpdateAtlas() {
  41.                 if (AtlasSwitch == true) {
  42.                         Debug.Log("---> Inside UpdateAtlas [true side]");
  43.                         ReplacementAtlas = Resources.Load("HomePanelTopBG") as GameObject;
  44.             Debug.Log("---> Setting texture GO to 'Clicked'");
  45.                 }
  46.                 else {
  47.                         Debug.Log("---> Inside UpdateAtlas [false side]");
  48.                         ReplacementAtlas = Resources.Load("TopBlackBar") as GameObject;
  49.             Debug.Log("---> Setting texture GO to 'Default'");
  50.                 }
  51.                
  52.                 Debug.Log("---> Replacing Atlas. ReferenceAtlas = "+ ReferenceAtlas + " ReplacementAtlas = "+ReplacementAtlas);
  53.                  ReferenceAtlas.replacement = ReplacementAtlas.GetComponent<UIAtlas>();
  54.                 Debug.Log("Atlas Replaced");
  55.         } // end UpdateAtlas()
  56. }

Error occurs when it won't actually load the resource "TopBlackBar", and halts at " ReferenceAtlas.replacement = ReplacementAtlas.GetComponent<UIAtlas>();" with the error.

Both atlases are located in the same folder and are named properly. Any idea what is happening that i'm unaware of?
Thanks for your time.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Atlas returning NullReference Exception
« Reply #1 on: July 25, 2012, 07:42:15 PM »
All I can suggest is look closely at what's different between them and to make sure you spelled the name correctly.

kwelts

  • Guest
Re: Atlas returning NullReference Exception
« Reply #2 on: July 25, 2012, 09:41:30 PM »
ok thanks for the response (at least i have something to focus on).

kwelts

  • Guest
Re: Atlas returning NullReference Exception
« Reply #3 on: July 26, 2012, 10:16:33 AM »
code is the same. it changes once but not back. i've gone through everything i can so im just going to post every little relevant part of my project and you can see if I missed something crucial.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Atlas returning NullReference Exception
« Reply #4 on: July 26, 2012, 04:25:33 PM »
I don't see anything wrong.

kwelts

  • Guest
Re: Atlas returning NullReference Exception
« Reply #5 on: July 26, 2012, 08:41:46 PM »
oh noes!

Godgib

  • Guest
Re: Atlas returning NullReference Exception
« Reply #6 on: August 09, 2012, 11:42:15 AM »
Ran into this problem yesterday.  Try renaming the actual atlas prefab to something that doesn't match the material and texture.  By default, NGUI creates all 3 with the same name, but this can confuse the Resource Load.