On collision enter, detect more than one tag?
void OnTriggerEnter(Collider Buddy)
{
if (Buddy.tag == "Coin");
{
points = points + 1;
Destroy(Buddy.gameObject);
}
if (Buddy.tag == "npc");
{
npcGuy.GetComponent<NPC>().NewMessage();
}
}
So I've got a coin that I want to collect and a NPC that I want to trigger a message when you move close to it. The message of the NPC is stored in a function in another script that is attached to the NPC itself. I'm not sure if the message is displayed on the screen or not because the object is destroyed when I collide with it even though it's not tagged a coin. What am I doing wrong?
// comment out the destroy and see if it still getting destroyed or what happens after that, it may be getting destroyed by some other area in the code, but the trgger looks fine, it shouldn't be running through coin unless that's the tag that's touched it
The function from the other NPC cube is called, so at least that's working but the coin tag is run too. I don't know why. It's only tagged npc...
Here is the entire script: using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class Player : $$anonymous$$onoBehaviour {
//Variables
public float speed = 10;
public float gravity = 10;
public float maxVelocityChange = 10;
public int Health = 10;
public int points = 0;
public float jumpHeight = 2;
public bool grounded;
private bool Dead;
private Transform PlayerTransform;
private GameObject Enemy;
private Rigidbody _rigidbody;
public Text text;
public GameObject npcGuy;
// Use this for initialization
void Start () {
PlayerTransform = GetComponent<Transform>();
_rigidbody = GetComponent<Rigidbody>();
_rigidbody.useGravity = false;
_rigidbody.freezeRotation = true;
}
// Update is called once per frame
void FixedUpdate () {
PlayerTransform.Rotate(0, Input.GetAxis("Horizontal")*2, 0);
Vector3 targetVelocity = new Vector3(0, 0, Input.GetAxis("Vertical"));
targetVelocity = PlayerTransform.TransformDirection(targetVelocity);
targetVelocity = targetVelocity * speed;
Vector3 velocity = _rigidbody.velocity;
Vector3 velocityChange = targetVelocity - velocity;
velocityChange.x = $$anonymous$$athf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = $$anonymous$$athf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
_rigidbody.AddForce(velocityChange, Force$$anonymous$$ode.VelocityChange);
if (Input.GetButton("Jump") && grounded == true)
{
_rigidbody.velocity = new Vector3(velocity.x, CalculateJump(), velocity.z);
}
_rigidbody.AddForce(new Vector3(0, -gravity * _rigidbody.mass, 0));
grounded = false;
}
private void Update()
{
text.text = ("Score: " + points);
}
float CalculateJump()
{
float Jump = $$anonymous$$athf.Sqrt(2*jumpHeight*gravity);
return Jump;
}
void OnTriggerEnter(Collider Buddy)
{
if (Buddy.tag == "Coin");
{
points = points + 1;
//Destroy(Buddy.gameObject);
}
if (Buddy.tag == "npc");
{
Debug.Log("Working");
npcGuy.GetComponent<NPC>().New$$anonymous$$essage();
}
}
void OnCollisionStay()
{
grounded = true;
}
}
Your answer
Follow this Question
Related Questions
Lose Health when GameObject enters collider? (OnTriggerEnter) 2 Answers
Why is OnTriggerEnter not working properly? 2 Answers
Function OnTriggerEnter2D is not called 1 Answer
How to deadtivate a gameobjects from the scene, if player has a character controller 0 Answers
Make object(s) stack vertically. 0 Answers