triggers/sripting issue

I made an object and when the mesh collides I want to change the texture.

I got that working fine by having a script attached and a static variable which becomes true when the character controller collides with it attached the to object it is colliding with.

However if i use the same script on another object a collision will change the texture on both objects not just the one it is colliding with (i want to put lots of these down and they change colour as the character steps on them).

So is there a way to just change the static variable of the script attached to the object rather than all the scripts in the scene? ( i am using unity/javascript).

Static variables are shared between all instances of the script/class/struct with which they’re associated. If you want to modify each individual object as the player steps on it, you’ll need to re-arrange your logic accordingly. (The most obvious solution would probably be to use a non-static member variable rather than a static member variable.)

I have realised I think my issue is I should be using a trigger rather than a collider, then I don’t need the variable to be a public.

The only answer to that is to not make it static.

I am still having a headache with this.

This does’t seem to work when I attach to an object.

You are still having a headache, because the code isn’t arranged like you want it to.

Step 1: Think about what you want.

“When the mesh collides I want to change the texture.”

Step 2: What tools do you have to make this happen? What is the logic I want to follow?

renderer.material.mainTexture
OnTriggerEnter()

“When the player enters the trigger, the texture needs to change!”

Step 3: Make the code follow your tools and logic.

var newTexture : Texture2D;

// This is a "buffer", or storage variable.  You want
// to keep the original material nice and safe, and
// only affect the object this is attached to.
private var myMaterial : Material;

// Start():
// Copy the object's material to the buffer, so we
// can mess with it however we want!!!
function Start() : void
{
  // Save this object's material to the private material.
  myMaterial = renderer.material;
}

// OnTriggerEnter():
// When the player enters the trigger region, change
// the texture.
function OnTriggerEnter() : void
{
  myMaterial.texture = newTexture;
  renderer.material = myMaterial;
}

Just take the time to think about what you want before you try it. A notepad or dry-erase board is a good investment, no matter what level programmer you are.

moving the code doesn’t change things. The debug.log line nevers runs. I can’t get the object to trigger at all.

Okay this is interesting. I create a sphere and add a rigid body to it and drop it on top it triggers, however when i run over it with the character controller it doesn’t trigger.

I must be missing something simple.

Just for clarity.

I am creating an object adding a collider and using a script attached to it to try and detect when the character controller touchs it.

If i turn the triggers off I can detect a collider it using the OnControllerColliderHit only if I attach it to the character controller object.

It is important to me it is attached to the object it is colliding with not the character controller for game logic reasons.

Further investigating I have determined that the character controller is definitly not triggering the trigger.

I attached a sphere with a rigidbody as a child and let the character controller drag it around it set the trigger off.

Sooo why isn’t my character controller setting off the trigger?

solved

for anyone interested the collider wasn’t big enough for the character controller but big enough for my other tests. So when I made it bigger it picked up the trigger with no problems.

thank god for that :smile: