Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ramsey111 · Jan 09, 2016 at 04:35 AM · c#texture

Trying to change the texture of an object, Pretty sure it is a simple mistake.

So I've been following this tutorial on youtube by EYEmaginary it's for a horror game. One of the first things you do in this game is make it so the screen of a tv changes textures when you get with in a certain distance of it using "OnTriggerEnter". I've been having some issues with this part so I created another project just to test code to make a cube change textures when ever I drag another cube towards it that's been tagged "player". I just press play then drag the second cube in the scene view and the collision is working it's just the texture change that isn't. the texture on the cube that's supposed to change goes white from the texture already set to the material, but won't change to the new texture. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class BoxMatChangeScript : MonoBehaviour {
     public Renderer rend;
     public Texture ninetyNine = new Texture();
     // Use this for initialization
     void Start () {
         ninetyNine = GetComponent<Texture> ();
         rend = GetComponent<Renderer> ();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
 
 
     void OnTriggerEnter(Collider other){
         other.gameObject.CompareTag ("Player");
         rend.material.mainTexture = ninetyNine;
     
     }
 }

If there's any info about the situation I'm missing just ask and I'll try to fill it in to the best of my ability.

EDIT: I don't know if this is obvious or not but I assign the textures to the variables through the inspector.

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

2 Replies

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

Answer by ClearRoseOfWar · Jan 10, 2016 at 03:21 AM

 public class BoxMatChangeScript : MonoBehaviour {
 
      public Material[] Materials;
      public Renderer rend;
      GameObject PlayerGO;
      //public GameObject PlayerGO; // You can drag the player into the inspector
 
      void Start () {
         PlayerGO = GameObject.FindWithTag("Player"); //remove this line if you drag the player into the inspector
         rend = GetComponent<Renderer> ();
      }
  
      void OnTriggerEnter(Collider other){
          if(other == PlayerGO){
             Material[] mats = rend.materials;
             mats[0] = Materials[0]; 
             rend.materials = mats;
          }
      }
  
  }
Comment
Add comment · Show 3 · 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 ClearRoseOfWar · Jan 10, 2016 at 03:54 AM 0
Share

Sorry, you'll need to add this to the top of your class:

 public $$anonymous$$aterial[] $$anonymous$$aterials;
avatar image Ramsey111 · Jan 10, 2016 at 05:31 AM 0
Share

Yeah that worked, thanks so much man!

avatar image ClearRoseOfWar Ramsey111 · Jan 10, 2016 at 05:57 AM 0
Share

No Problem. Happy to help :)

avatar image
0

Answer by Ramsey111 · Jan 10, 2016 at 03:25 AM

@DLively Quite possibly, I'll check right now.

There are two things I want to ask about: The first of which is could you possibly explain the bits and pieces of this code? Just so I can understand it better. And also the compiler gives me an error on the second line with the word Materials as in Materials[1]; it gives the error:

Assets/BoxMatChangeScript.cs(24,27): error CS0103: The name `Materials' does not exist in the current context

now it says the error:

IndexOutOfRangeException: Array index is out of range. BoxMatChangeScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/BoxMatChangeScript.cs:25)

So I changed the script and got the same error. I don't know what's wrong maybe you'ed understand, Here's my script now with your addition:

 using UnityEngine;
 using System.Collections;
 
 public class BoxMatChangeScript : MonoBehaviour {
     public Material[] Materials;
     public Renderer rend;
     public Texture ninetyNine = new Texture();
     // Use this for initialization
     void Start () {
         ninetyNine = GetComponent<Texture> ();
         rend = GetComponent<Renderer> ();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
 
 
     void OnTriggerEnter(Collider other){
         other.gameObject.CompareTag ("Player");
         //rend.material.mainTexture = ninetyNine;
         Material[] mats = GetComponent<Renderer>().materials;
         mats[0] = Materials[0]; GetComponent<Renderer>().materials = mats;
     }
     /*
     Material[] mats = GetComponent<Renderer>().materials;
     mats[0] = Materials[1]; GetComponent<Renderer>().materials = mats;
     */
 
 }


Could the issue be that I'm working with textures in my script?

Comment
Add comment · Show 4 · 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 ClearRoseOfWar · Jan 10, 2016 at 04:10 AM 0
Share

IndexOutOfRangeException: Array index is out of range.

this means you haven`t added 2 or more array slots in your inspector.

Remember : when refering to arrays you start from 0. 0 = 1; 1 = 2; 2 = 3;

If you only need the one slot, then just change $$anonymous$$aterials[1] to 0

avatar image ClearRoseOfWar · Jan 10, 2016 at 04:37 AM 0
Share

Looks like it should be working, I have the same code working in my script right now... This is all its doing:

 $$anonymous$$aterial[] mats = GetComponent<Renderer>().materials; // get the current materials from your gameobject
  mats[0] = $$anonymous$$aterials[0];// renderer 'slot 0' is set to the material you stored in inspector in 'slot 0'
 GetComponent<Renderer>().materials = mats;// apply your changes back to the renderer
avatar image ClearRoseOfWar · Jan 10, 2016 at 04:53 AM 0
Share

Yes, you need to use materials Textures alone will not work

avatar image ClearRoseOfWar · Jan 10, 2016 at 05:11 AM 0
Share

I changed my answer above to a clean version of your code.. it should work.

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

58 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to partially change texture? 0 Answers

Terrain Ground, lerp Grass and Snow in Cities: Skylines in C# 0 Answers

Road Sign visibile from both sides 1 Answer

Textures not Appearing When Loading from Different Scene 0 Answers

Check if the player is off/on the path. (3D; c#) 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