Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Maybe this is a bug ...

Discussion in 'Scripting' started by Shudrum, Feb 18, 2015.

  1. Shudrum

    Shudrum

    Joined:
    Apr 3, 2011
    Posts:
    63
    Hi all, Hi dev team,

    Working on my project, and trying to access via script to a UI.Text child of a UI.RectTransform ... NullReference ... This is a realy simple RectTransform with a Button component with another RectTransform as child with a Text component.

    BUT !

    Code (csharp):
    1.  
    2. // Return null :
    3. buttonRectTransform.GetComponentInChildren<Text>() as Text;
    4.  
    5. // Return my Text object :
    6. buttonRectTransform.GetChild(0).GetComponent<Text>() as Text;
    7. // linkedBuyTransform.GetChild(0).GetComponent<Text>() as Text; (Before edit)
    8.  
    Is it a bug ? Maybe I'm wrong ...
     
    Last edited: Feb 19, 2015
  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Try removing the casts. I don't think those are necessary and it could be causing issues.
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    does buttonRectTransform have children?
    Your example wouldve been more convincing if you had used the same reference each time
     
    jtsmith1287 likes this.
  4. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Ya I had the same thought too, but gave him the benefit of the doubt, haha.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The casts aren't necessary but will never cause issues. If you get a null reference exception, that means the object you're attempting to reference doesn't exist. It's not a Unity bug.

    --Eric
     
  6. Shudrum

    Shudrum

    Joined:
    Apr 3, 2011
    Posts:
    63
    My bad ...

    This is exactly the same object (those two lines are really side by side, no modifications between them) :

    Code (csharp):
    1.  
    2. // Return null :
    3. buttonRectTransform.GetComponentInChildren<Text>() as Text;
    4.  
    5. // Return my Text object :
    6. buttonRectTransform.GetChild(0).GetComponent<Text>() as Text;
    7.  
    (linkedBuyTransform is my script var name, renamed to buttonRectTransform for the forum, just missed one replace)

    And the scructure is really simple as I've said :

    RectTransform with a Button
    +- RectTransform with a Text

    And yes, the cast is useless, but I've tried with it ... just in case.
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Stopped being lazy, tested for myself:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.     public Transform otherTransform;
    7.  
    8.     void Start () {
    9.         Debug.Log( otherTransform.GetComponentInChildren<Text>() );
    10.         Debug.Log( otherTransform.GetChild( 0 ).GetComponent<Text>() );
    11.     }
    12. }
    13.  
    No errors (Unity 4.6.2f1)

     
    jtsmith1287 likes this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In the general case as casts can cause null reference errors, because as will return null if the casting is impossible. However in this case the casting will not cause the error, just burn through cpu cycles.
     
  9. Shudrum

    Shudrum

    Joined:
    Apr 3, 2011
    Posts:
    63
    Hey, thank you hpjon, exactly what I mean, but you've got the right result ><

    Do not understand why ... I'll try to find out why ...

    Thank you !