Hey guys, I’m still very new to scripting and I’m trying to get the hang of it all. I’m trying to write a script so that when the player enters a trigger box that has been parented to a floor tile, it will simply change the color of the parented floor tile to a different color ( in this case gray ), but I’m having trouble getting it to work. Please look at my code and tell me what I’m doing wrong!
using UnityEngine;
using System.Collections;
public class FloorTrigger : MonoBehaviour {
private Renderer rend;
void Start()
{
rend= GetComponentInParent<Renderer>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Trigger"))
{
rend.material.color=Color.gray;
}
}
}
When I load up Unity, it doesn’t tell me I have any compiler errors, but it doesn’t tell me what to do, so I would like to know what I need to add to my code to make it run correctly!
A couple of thoughts: if this is a trigger script on the floor object, then the collider has to be marked trigger.
Also, the question of what tag is the “other” object, that should be the Player, right? The player is tripping it, so that’s the "other’ that enters this trigger. I wouldn’t think you would need to have a Tag called “Trigger,” just make sure the collider is marked trigger.
Finally, take a look at debugger tutorials, attach the debugger via Mono, and see if the OnTriggerEnter function is being called. That will tell you how far you are getting.
Thanks for your help! However, I still can’t quite get it to work.
I attached my OnTriggerEnter code to my ball movement script since this is what the game will consider to be the player. I then increased the size of the trigger to see if it would help and included a Debug.Log line of code in the script to help determine if the function is being called. I know my function is being called properly, however I see no color change. I’m clueless as to how I can get this to work.
Here are my changes as mentioned:
using UnityEngine;
using System.Collections;
public class BallMovement : MonoBehaviour {
public int speed = 100;
private Rigidbody rb;
private Renderer rend;
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
// Use this for initialization
void FixedUpdate ()
{
float moveHorizontal = -Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveVertical, 0.0f, moveHorizontal);
rb.AddForce (movement * speed);
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Trigger"))
{
rend = gameObject.GetComponentInParent<Renderer>();
rend.material.color = Color.gray;
Debug.Log ("This is Working");
}
}
}
Well you’re on the right track to add more Debugging. Add in something as the first line of OnTriggerEnter that prints the name and/or tag field out.
The other thing is to print the name of the other.gameObject.name, see if that is “who you think it is.”
And also, just to be sure, the Ball/player has a collider too, right??
One other thought: change the rend.material.color = Color.gray call to something like so:
Material tempMtl = rend.material;
tempMtl.color = Color.gray;
rend.material = tempMtl;
Materials and assignations in Unity have some unexpected duplication and deconfliction rules so that you can set one object’s color and not have it effect every other… I forget the details exactly.