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 Triqy · Sep 29, 2013 at 10:53 PM · texturetexture2dcombineapply

Applying Textures from atlas?

Im trying to apply a simple 2d texture to a cube from my atlas script.

 #pragma strict
 
     // Source textures.
 var atlasTextures: Texture2D[];    
     // Rectangles for individual atlas textures.
 var rects: Rect[];
 
 function Start () {
         // Pack the individual textures into the smallest possible space,
         // while leaving a two pixel gap between their edges.
         var atlas = new Texture2D(8192, 8192);
         rects = atlas.PackTextures(atlasTextures, 2, 8192);
         
         print(atlasTextures[0]);
        // atlasTextures[0] = GameObject.Find("Cube1");
        
         var temp = transform.Find("Cube1");
         atlasTextures[0] = temp.renderer.material.mainTexture;
     }

this is what i got...I think the last 2 lines need to change.. they are throwing nullreference exceptions. Any direction would be greatly appreciated. Thanks!

Comment
Add comment · Show 3
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 Triqy · Sep 29, 2013 at 11:39 PM 0
Share

Bump!!!!!!

avatar image Hoeloe · Sep 30, 2013 at 12:17 AM 0
Share

There was no need to bump after half an hour. As for your question - try using some Debug.Log statements to find out which of those is returning null. To throw a NullReferenceException, it has to be at least one level in, so it's not atlasTextures[0] that is null, nor is it mainTexture. It could be the atlasTextures array itself, but I'm assu$$anonymous$$g you're setting that in the inspector. It could also be temp, temp.renderer or temp.renderer.material. Try adding these lines to your code:

 Debug.Log(atlasTextures);
 Debug.Log(temp);
 Debug.Log(temp.renderer);
 Debug.Log(temp.renderer.material);

This should deter$$anonymous$$e which of these is null, which will help to discover the cause of the problem.

avatar image Triqy · Sep 30, 2013 at 01:04 AM 0
Share

Ok i ran the Debugs and it said null at the temp var?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Sep 30, 2013 at 01:38 AM

transform.Find only searches in the object's children - if "Cube1" isn't a child of the object this script is attached to, temp will be set to null and generate Null Reference Exception. Use GameObject.Find instead:

    ...
    var temp = GameObject.Find("Cube1");
    atlasTextures[0] = temp.renderer.material.mainTexture;
 }

EDITED: PackTextures merges the textures passed in the array into a single texture and returns an array of Rects that shows the location of each texture in the atlas. In order to use the packed textures, assign the atlas to the mainTexture and define the texture offset and scale according to the corresponding rect. If you want to use the packed version of atlasTextures[2], for instance, do the following:

 ...
 var temp = GameObject.Find("Cube1");
 temp.renderer.material.mainTexture = atlas;
 temp.renderer.material.mainTextureOffset = Vector2(rects[2].x, rects[2].y);
 temp.renderer.material.mainTextureScale = Vector2(rects[2].width, rects[2].height);


Comment
Add comment · Show 8 · 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 Triqy · Sep 30, 2013 at 01:53 AM 0
Share

That fixed the nullrefexception but now im getting another error that says invaildCastException... Sorry for the trouble. its just ive never seen these types of errors.

avatar image Triqy · Sep 30, 2013 at 01:54 AM 0
Share

cannot cast from source type to destination..

avatar image Triqy · Sep 30, 2013 at 01:57 AM 0
Share

What it is doing though is creating a instance material but not applying the texture to it...The material on Cube1 says Default-Diffuse(Instance)

avatar image Triqy · Sep 30, 2013 at 02:05 AM 0
Share

Never$$anonymous$$d i fixed it! Heres the code i used to apply textures to a model from an atlas! Thanks @aldonaletto for your direction.

 #pragma strict
 
     // Source textures.
 var atlasTextures: Texture2D[];    
     // Rectangles for individual atlas textures.
 var rects: Rect[];
 
 function Start () {
         // Pack the individual textures into the smallest possible space,
         // while leaving a two pixel gap between their edges.
         var atlas = new Texture2D(8192, 8192);
         rects = atlas.PackTextures(atlasTextures, 2, 8192);
               
         var temp = GameObject.Find("Cube1");    
         
         temp.renderer.material.mainTexture = atlasTextures[0]; 
         
 
     }
avatar image aldonaletto · Sep 30, 2013 at 03:20 AM 0
Share

But notice that you're using the original texture, not the one packed in the atlas. In order to use the atlas texture, you must assign atlas to the texture and set its offset and scale according to the rect returned by PackTextures. Take a look at my answer: I edited it and added the code to effectively use the atlas.

Show more comments

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

17 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

Related Questions

Texture.Apply() Crashes at Resolutions under 1920x1080 1 Answer

Translate a pixel using SetPixel on a Texture2D? 1 Answer

GUI texture to change texture when hovered over or clicked? 2 Answers

How do you crop a Texture2d 1 Answer

Can Unity utilize textures created by a plugin? 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