This has me stumped. I’m able to successfully change the texture of an entity, lets say for this example a cube, when my Player collides with the cube. However, I want to change the texture for my Player, not the cube. I think what is throwing me off, is that the texture is attached to a child of the overall Player entity.
Here is my code for changing the Cube’s texture.
using UnityEngine;
using System.Collections;
public class changeTex : MonoBehaviour {
public Texture texturea;
public Texture textureb;
public Collider other;
// Use this for initialization
void Start () {
renderer.material.mainTexture = texturea;
}
void OnTriggerEnter(Collider other) {
this.other = other;
//other.tag="Player";
if(other.tag!="Player") {
print ("nice try");
} else {
renderer.material.mainTexture = textureb;
}
}
}
You can see that the Player has to have a tag of “Player” for it to work. Now, my code where instead of changing the Cube’s texture, you change the Player’s texture.
using UnityEngine;
using System.Collections;
public class changeToHelmColor : MonoBehaviour {
public Texture normTexture;
public Texture changeTexture;
public Collider other;
// Use this for initialization
void Start () {
renderer.material.mainTexture = normTexture;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) {
this.other = other;
if(other.tag != "Helm") {
print("Nice try");
} else {
renderer.material.mainTexture = changeTexture;
}
}
}
From what you see, upon colliding with the cube, it will change the texture. I have selected the Cube’s tag as Helm, because I want this script to change the player’s helm texture.
What is happening though, is that upon loading, it immediately prints “Nice try”, and doesn’t update when I do collide with the cube.