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
-1
Question by allanmb · Jul 12, 2013 at 03:59 PM · texture2dnullreferenceexceptionongui

Texture2D is Null even though hooked up in Inspector.

I am trying to draw a 2D texture as part of a reticule in my game. I have a Texture2D in my script and have it hooked up to a png in the inspector. When I try to use it I just get Null reference exceptions. I can programatically make a texture which works and I can see the quad I want but I want to use the png texture. This is the basics of what I have: public class Ship : MonoBehaviour { public Texture2D crosshair;

 Texture2D sameTeamTexture;
 Texture2D enemyTeamTexture;

 void Start()
 {
     /*sameTeamTexture = new Texture2D(10, 10);    
     for(int i =0; i < sameTeamTexture.width; ++i)
     {
         for(int j=0; j < sameTeamTexture.height; ++j)
         {
             sameTeamTexture.SetPixel(i, j, new Color( 1, 1, 1, 1 ) );
         }
     }
     sameTeamTexture.Apply();
     
     enemyTeamTexture = new Texture2D(10, 10);    
     for(int i =0; i < enemyTeamTexture.width; ++i)
     {
         for(int j=0; j < enemyTeamTexture.height; ++j)
         {
             enemyTeamTexture.SetPixel(i, j, new Color( 1, 0, 0, 1 ) );
         }
     }
     enemyTeamTexture.Apply();*/
 }
 
 void OnGUI()
 {
     DrawCrosshair( Vector3.zero, 100.0f, true, false );
     
     var players = PlayerTracker.GetAllPlayerInfos();
     var myPlayerInfo = PlayerTracker.MyPlayerInfo;
     foreach( PlayerInfo player in players )
     {
         if ( player != myPlayerInfo )
         {
             Vector3 screenPos = Camera.mainCamera.WorldToScreenPoint( player.transform.position );
             float border = 100.0f;
             DrawCrosshair( screenPos, border, false, player.PlayerTeam == myPlayerInfo.PlayerTeam );
         }
     }
 }
 
 private void DrawCrosshair( Vector3 screenPos, float border, bool drawWhenVisible, bool sameTeam )
 {
     // snap position and display texture
     screenPos.x = Mathf.Clamp( screenPos.x, border, Camera.mainCamera.GetScreenWidth() - border );
     screenPos.y = Mathf.Clamp( screenPos.y, border, Camera.mainCamera.GetScreenHeight() - border );
     
     /* debug - draw quad
     Texture2D tex;
     if ( sameTeam )
     {
         GUI.color = new Color( 1, 1, 1, 1 );
         tex = sameTeamTexture;
     }
     else
     {
         GUI.color = new Color( 1, 0, 0, 1 );
         tex = enemyTeamTexture;
     }
     GUI.DrawTexture(new Rect( screenPos.x,
                          Camera.mainCamera.GetScreenHeight() - screenPos.y, 
                         tex.width,
                         tex.height), 
                         tex, ScaleMode.StretchToFill, false);*/
                         
     GUI.DrawTexture(new Rect( screenPos.x - (crosshair.width * 0.5f),
                          screenPos.y - (crosshair.height * 0.5f), 
                         crosshair.width,
                         crosshair.height), 
                         crosshair, ScaleMode.ScaleToFit, false);
 }

What am I failing to do to get this working? Thanks Allan

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 robertbu · Jul 12, 2013 at 04:00 PM 0
Share

Usually when this happens, you will find that you have the script attached to two different object, one with the value assigned in the inspector, and one still unassigned.

avatar image allanmb · Jul 15, 2013 at 02:29 PM 0
Share

Thanks, I got it working. I was adding the textures to the instance of the prefabs ins$$anonymous$$d of the base prefab. Now it is working :-)

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

15 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

Related Questions

Handling NullReferenceException in EditorWindow OnGUI() 0 Answers

Mathf.Lerp 2 Answers

Create Texture2D elements in OnGUI Method 1 Answer

Constant Rotation with GUI.RotateAroundPivot 1 Answer

NullReferenceException in a GUI Dialog Method 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