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.
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;
}
}
}
Sorry, you'll need to add this to the top of your class:
public $$anonymous$$aterial[] $$anonymous$$aterials;
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?
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
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
Yes, you need to use materials Textures alone will not work
I changed my answer above to a clean version of your code.. it should work.