• 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
0
Question by Dakcenturi · Oct 25, 2012 at 09:00 PM · texturereference

How do I assign a texture to a gameobject in script?

I am trying to assign a texture to an already existing game object. Basically what I am trying to accomplish is two buttons, when one button is pressed it makes uses one texture, then when the other button is pressed it switches to the other texture.

This is basically simulating switching the picture on the menu on button press.

Below is the code I have. It compiles fine, but when I run it I get: NullReferenceException: Object reference not set to an instance of an object

I'm assuming this is on the travelBoard. In unity I dragged the texture to the travelBoard reference. I thought this would create the reference. Do I actually have to setup the reference in the code (for example in the Start section)? If so how do I get just the texture? I tried .GetComponent(Texture) but that doesn't seem to work either.

 using UnityEngine;
 
 public class ChangeTexture2 : MonoBehaviour
 {
     UITexture Board;
     public Texture travelBoard;
     
     void Start ()
     {
         
     }
  
     void OnPress()
     {
             Board.mainTexture = travelBoard;    
     }
 }

alt text

unity1.jpg (50.0 kB)
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 Freaking-Pingo · Oct 25, 2012 at 09:20 PM 0
Share

I think your texture reference is fine. What about the Board.mainTexture you are assigning your texture to? What object is UITexture?

avatar image AMDAndrew · Oct 25, 2012 at 09:30 PM 0
Share

It is simple. Ins$$anonymous$$d of = texture you must have = "texture/path"

avatar image hvilela · Oct 26, 2012 at 01:08 AM 0
Share

I'll assume you're using NGUI, based on the scripts structure. If you do, you should say that in the description and I recommend you to post your question at the NGUI's forum, cause you'll have better responses there.

2 Replies

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

Answer by Maulik2208 · Nov 22, 2012 at 04:17 PM

Gameobject.renderer.material.mainTexture = Texturename;

Hope this will work For you.....Enjoy

Comment
Add comment · 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
0

Answer by Montraydavis · Oct 26, 2012 at 01:15 AM

Define the renderer, and then

void SetMat(int nIdx) { renderer.material = m_aMaterials[nIdx]; }

Found this on the forum, and works.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Texture Reference Objects 0 Answers

Does reference to Texture count on RAM usage of device? 0 Answers

Temporary textures or Materials in Editor removed in build 1 Answer

Make reference to the project texture file in the material field 1 Answer

Null Reference Exception After refering to an Array 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