- Home /
Trigger Effect Error
Hello, i have a trigger enter script that only seems to work when i attach it to the floor tiles and the player but i want to only want it to be attached to the floor tiles becuase if its attached to both the floor tiles and the players it will create problems in the future. heres my script
using UnityEngine;
using System.Collections;
public class Action : MonoBehaviour {
void OnTriggerEnter(Collider other){
Movement2 m = (Movement2)GetComponent("Movement2");
m.AdjustCurrentAction(-1);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
The floor tile has a box collider and a mesh renderer attached to it
the player has a mesh renderer, box collider and rigidbody added to it
please help thanks in advance
sry if that didnt make sense, okay so i have a chess like type of game and i want the player for example to move a certain number of tiles away. So i have created a script that when the player enters a tiles trigger it takes away one point from the players action points. but in order for that script to work it needs to be attached to the tile and the player. But since some tiles take away more action points than others, having the script attached to the player is a problem. So i was wondering if you could tell me what i could do to with this script so that it doesnt need to be attached to the player in order for it to work. P.S. the tile has a box collider and mesh renderer added to that and the player has a mesh renderer, box collider, and rigidbody added to it
Answer by Anusha · Sep 04, 2012 at 08:42 AM
remove the script from player... then tag ur player as "player". now only ur tile has this script attached to it. so now when ur player moves over the tiles this will trriger
void OnTriggerEnter(Collider other)
{
if(other.transform.tag == "player")
{
GameObject m_Player = GameObject.Find("player");//assuming ur player is named player
Movement2 m = (Movement2)m_Player.GetComponent("Movement2");
m.AdjustCurrentAction(-1);
}
}
}
what this is doing is...when any collider enters the tile,it checks whether its the player, if yes then it finds ur player and get the Movement2 script and calls the m.AdjustCurrentAction(-1) function.
Thanks! but when i used ur script unity said m_player doesnt exsist in the current context
sorry typo mistake.. change the below line... its m_Player...
$$anonymous$$ovement2 m = ($$anonymous$$ovement2)m_Player.GetComponent("$$anonymous$$ovement2");