Hello i have a code here, whats its suppose to do is when something enters the trigger effect, it checks the game object that enter and then did what its suppose to do to that game object. But it doesnt seem to be working. heres my script
using UnityEngine;
using System.Collections;
public class Action : MonoBehaviour {
public GameObject Knight;
//other gameobjects will be here in the future
private bool hasEntered = false;
void OnTriggerEnter(Collider other)
{
if(hasEntered)
return;
KnightMovement2 km = (KnightMovement2)Knight.GetComponent("KnightMovement2");
km.AdjustCurrentAction(-1);
hasEntered = true;
}
if(hasEntered = true){
check GameObject{
if( GameObject Knight){
KnightMovement km = (KnightMovement)Knight.GetComponent("KnightMovement");
km.AdjustCurrentAction;
//and so on with the other gameobjects. im pretty sure i messed up ^ this part could you help me fix it thanks in advance
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
There are several ways to check which object entered the trigger. If there’s only one Knight object in scene, you can drag it to a public variable in the trigger script, as your script suggests:
using UnityEngine;
using System.Collections;
public class Action : MonoBehaviour {
public GameObject Knight; // drag the knight here in the Inspector
bool knightEntered = false;
public GameObject Queen; // other object
bool queenEntered = false
// and so on
void OnTriggerEnter(Collider other)
{
// if it's the knight, and it's the first time
if (other.gameObject == Knight && !knightEntered){
knightEntered = true; // tell that Knight entered trigger
KnightMovement2 km = other.GetComponent< KnightMovement2>();
km.AdjustCurrentAction(-1);
}
if (other.gameObject == Queen && !queenEntered){
queenEntered = true; // tell that Queen entered trigger
QueenMovement2 qm = other.GetComponent< QueenMovement2>();
qm.AdjustCurrentAction(-1);
}
// and so on
}
}
On the other hand, if you may have several Knights and Queens, you could use specific tags for each type, like this:
using UnityEngine;
using System.Collections;
public class Action : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.tag == "Knight"){
KnightMovement2 km = other.GetComponent< KnightMovement2>();
km.AdjustCurrentAction(-1);
}
if (other.tag == "Queen"){
QueenMovement2 qm = other.GetComponent< QueenMovement2>();
qm.AdjustCurrentAction(-1);
}
// and so on
}
}