- Home /
Trying to Disable Compenent when there's a collsion
Im trying to tell Unity that im the character collides with a certain area then it should disable the character controller. But i cant seem to disable the Compnenet or the Script.... PLEASE HELP
private var Controller: PlayerControls;
function OnCollisionEnter2D (collider : Collider) {
if (collider.gameObject.tag == "GoalFieldR") {
Controller.enabled = false;
}
}
Please show us PlayerControls script so we can detect the problem.
private var screenPoint: Vector3;
private var offset: Vector3;
private var curScreenPoint : Vector3;
private var curPosition : Vector3;
function Start () {
}
function Update () {
}
function On$$anonymous$$ouseDown () {
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Screen.showCursor = false;
}
function On$$anonymous$$ouseDrag() {
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
function On$$anonymous$$ouseUp(){
Screen.showCursor = true;
}
Answer by sterynkng14 · Mar 02, 2014 at 01:15 PM
Well first off in this snippet it does not seem as if you are even defining Controller. Based on the information we have so far here you are. This obviously will only work if attached to the parent of the component you're trying to reach.
private var Controller : PlayerControls;
function Start()
{
Controller = GetComponent(PlayerControls);
}
function OnCollisionEnter2D (collider : Collider)
{
if(collider.gameObject.tag == "GoalFieldR")
{
Controller.enabled = false;
}
}
Answer by Nick4 · Mar 02, 2014 at 01:10 PM
As far as I'm aware you can't just use "enabled" variable if you haven't defined one in your script. I encourage you to define one.
Your PlayerControls script :
public var permission : boolean;
private var screenPoint: Vector3;
private var offset: Vector3;
private var curScreenPoint : Vector3;
private var curPosition : Vector3;
function Start () {
permission= true;
}
function Update () {
}
function OnMouseDown () {
if(permission)
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Screen.showCursor = false;
}
}
function OnMouseDrag() {
if(permission)
{
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
function OnMouseUp(){
Screen.showCursor = true;
}
Then you can access "permission" variable like this :
private var Controller : PlayerControls;
function OnCollisionEnter2D (collider : Collider) {
if (collider.gameObject.tag == "GoalFieldR") {
Controller.GetComponent<PlayerControls>().permission = false;
}
}
O$$anonymous$$G Thank you sooo much people like you are amazing !! True inspiration
If you could mark it as best answer, that would be amazing, too :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Analysing player metrics help 0 Answers
Check if an object is at a relative position? 2 Answers
Damage on collision problems... 0 Answers
HELP PLEASE! 3 Answers