- Home /
Change Main Texture on Collider not Collidee
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.
Is it possible to modify Unity so it doesn't create a Skinned $$anonymous$$esh Render? I'm pretty sure that's what's throwing it off.
Just this code with a Cube with a two temporary textures. It changes as its supposed to which is reason for me to believe that it is in fact a Skinned $$anonymous$$esh Renderer bug.
Answer by DaveA · Mar 20, 2012 at 07:33 PM
Set a breakpoint there and see what other.tag really is. Obviously it's not Helm. You would have to either put this script on the part of the player that needs to change (unless the entire player only has one mesh), or you would have this script find the sub-part and get its renderer and change it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065343im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't add colliding game objects to a list. 1 Answer
Access other collider without use of Trigger collider. 1 Answer
MonoBehaviour.OnTriggerX vs Collider.OnTriggerX 1 Answer
OnTriggerEnter not being called with multiple colliders 1 Answer
Best practice for checking which object has collided. 2 Answers