Search Unity

GUITexture from script?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Kulver, Feb 23, 2009.

  1. Kulver

    Kulver

    Joined:
    Jan 11, 2009
    Posts:
    20
    How to create GUITexture from script (c#)?

    public class GHUD : MonoBehaviour {
    private GUITexture gtxt = new GUITexture();

    void Awake() {

    DebugConsole.Log("object: "+gtxt );

    In console: "object: UnityEngine.GUITexture", so it's looks like it is created...

    gtxt.transform.position = new Vector3(0,0);
    gtxt.texture = someImage;

    when I try to access transform or texture Unity throws NullReferenceException
    what's wrong ???
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Not sure why texture is failing but you should be defining a proper Vector3 (0,0,0).


    Also, I'm moving this to the GUI section in 3... 2... 1...
     
  3. Kulver

    Kulver

    Joined:
    Jan 11, 2009
    Posts:
    20
    Vector3 has nothing to do with that error.... (btw, in docs there is Vector3 (x : float, y : float) constructor )

    Just for test I have created New Project , created Empty GameObject and attached the following script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.     public GUITexture myText;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         myText = new GUITexture();
    11.         print("object:"+myText);
    12.         print("pos:"+myText.transform.position);
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.     }
    19. }
    20.  
    access to myText.transform.position throws:
    NullReferenceException
    NewBehaviourScript.Start () (at Assets/NewBehaviourScript.cs:11)

    is it a bug in the Unity ??
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    GUITexture is only a component, not a game object.

    So what you would do is add a new game object to the world and instead use AddComponent to add the guitexture properly to the game object in question.
    then you will also have the possibility to access the other game objects components that are present commonly like transform etc

    Generally you would never create Components through new, always through AddComponent
     
  5. Kulver

    Kulver

    Joined:
    Jan 11, 2009
    Posts:
    20
    Thanks you! I had that thought , but when you look into documentation and see that GUITexture has all these Inherited Variables you get the idea that it should contain all transforms,etc....quite a mess in docs or in the class tree...

    a bit later...

    It is working like that, but you have to set scale to:
    gtxt.transform.localScale = new Vector3( 0.0F ,0.0F ,1.0F );
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    <facepalm> Sorry for missing that obvious point in my first reply...
     
  7. reaksmey_than

    reaksmey_than

    Joined:
    Aug 7, 2014
    Posts:
    1
    //...example want to create cube and add ur component with GUITexture you should follow this....
    void CreateCube(){
    cube=GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.AddComponent<GUITexture>();
    }