- Home /
High Jump / Speed problem in Trigger - best way to do this.
Welcome, I would like to make a high jump and speed zones in my project. I am looking for the best way to do this.
I was tried to make this by changing extraHeight variable in CharacterMotor script from other script attached to trigger using GetComponent but I received:
Object reference not set to an instance of an object
What is wrong? What is the best way to do this?
Here is my part of script:
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player"){
Debug.Log("Enter!"); //work fine
GetComponent<CharacterMotor>().jumping.extraHeight=12.0f; //didn't work
}
}
Cheers!
There is help at this Post. Do SEARCH for:: $$anonymous$$ake an object increase speed when entering trigger
Thank you for reply, could you recommend me something other than rigid body ? What I am doing wrong? $$anonymous$$aybe there are a simple solution to change character motor script?
Hello, could anybody help? Any solution on web didn't work.
Could anybody tell how to change the variables in Character$$anonymous$$otor from other scripts? What's wrong?
Answer by MOROZAW · Dec 09, 2013 at 11:00 PM
using UnityEngine;
using System.Collections;
public class jump : MonoBehaviour {
CharacterMotor playerScript;
// Use this for initialization
void Start () {
playerScript = (CharacterMotor)GameObject.FindGameObjectWithTag("Player").GetComponent("CharacterMotor");
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player"){
Debug.Log("Enter!"); //work fine
playerScript.jumping.baseHeight=10;
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.tag == "Player"){
Debug.Log("Exit!"); //work fine
playerScript.jumping.baseHeight=1;
}
}
void OnTriggerStay(Collider other) {
if (other.gameObject.tag == "Player"){
Debug.Log("Stay!"); //work fine
}
}
}
Here is my solution but could anybody check this? It works but I don't know why I must use this line:
(CharacterMotor)GameObject.FindGameObjectWithTag("Player").GetComponent("CharacterMotor");
Could somebody tell why I must use "(CharacterMotor)GameObject" and "GetComponent("CharacterMotor")" as string?
This is the best way or I can write it more simply?