Access variable from different class
I am making a power up for my player to double up its speed if it enter's the power up prefab's Collider . There are no errors popping up but when i enter the collider of the power up prefab still there is no change in the speed . check my code and help me out to spot the error . Any help is appreciated.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour{
public float movespeed= 12f;
public float turnspeed = 130f;
void Update () {
if (Input.GetKey (KeyCode.UpArrow)) {
transform.Translate (Vector3.forward * movespeed * Time.deltaTime);
}
if(Input.GetKey (KeyCode.DownArrow)) {
transform.Translate(-Vector3.forward * movespeed * Time.deltaTime);
}
if(Input.GetKey (KeyCode.LeftArrow)) {
transform.Rotate(Vector3.up * -turnspeed * Time.deltaTime);
}
if(Input.GetKey (KeyCode.RightArrow)) {
transform.Rotate(Vector3.up * turnspeed * Time.deltaTime);
}
}
}
Power up code
using UnityEngine;
using System.Collections;
public class Powerup : MonoBehaviour {
private Vector3 orginalscale;
protected Collider collider;
public float rotation_speed =50f;
public float up_down_speed =0.3f;
private float speedMultiplier = 2.0f;
private Movement movement ;
void Start () {
orginalscale = transform.localScale;
collider = GetComponent<Collider>();
}
void Update () {
transform.Rotate(new Vector3 (0, Time.deltaTime * rotation_speed, 0));
Vector3 position = transform.position;
position.y -= Mathf.Sin(Time.timeSinceLevelLoad - Time.deltaTime) * up_down_speed;
position.y += Mathf.Sin(Time.timeSinceLevelLoad) * up_down_speed;
transform.position = position;
}
void OnTriggerEnter(Collider other) {
collider.enabled = false;
transform.localScale = Vector3.zero;
GameObject thePlayer = GameObject.Find("enemytank1");
movement=thePlayer.GetComponent< Movement>();
movement.movespeed *= speedMultiplier;
}
}
yes i have the collider checked as trigger and the player has a rigid body.
why lookup player with Find rather than going through the other collider?
I am a newbie in unity, i first tried to use the other collider but it gave me errors so i used the way which i know better
@Aj_112 What errors did it give you? Are you sure the collider is even active when the player collides with it (since you disable it)?
Your current setup means: If ANYTHING collides with the object (even terrain) then it will disable itself.
Answer by sk8terboy4 · Mar 08, 2016 at 12:15 AM
@Aj_112 I assume you can call the triggers fine.
void OnTriggerEnter(Collider other) {
collider.enabled = false;
transform.localScale = Vector3.zero;
//GameObject thePlayer = GameObject.Find("enemytank1");
//movement=thePlayer.GetComponent< Movement>();
// movement.movespeed *= speedMultiplier;
if(other.gameObject.tag == "player"){
other.gameObject.GetComponent<Movement>().movespeed *= speedMultiplier;
}
}
Just create a new tag called "player" and set that tag on the player object which has the Movement script attached to it. Once you pass through the trigger, the player speed should be doubled by speedMultiplier, keep in mind the speed is not going to decrease back to normal unless you specify otherwise. I don't know if this code will work though.
I made a test scene and it seems to work fine. I think you need to increase the speed multiplier to say 10 in order to see a change in speed. On my capsule I had the movement script, a rigidbody, and a capsule collider. The capsule also had a tag called "player". On the Powerup object, I had the Powerup script and a box collider with Is Trigger checked. link text
wow, I really appretiate your help ;) . Yes you are right the code does work . i also tried in another empty project with a cube and when it took the powerup it increased the speed . But it is't working in my main project . I read that by taking a null reference can help but i have no idea where and how to take a null reference.
Your answer
Follow this Question
Related Questions
C# OnTriggerEnter Issue 3 Answers
OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers
Crane Pick up script - setting new parent 0 Answers
OnTriggerEnter Issue - Collider problem 0 Answers
How to call an action from a script that is on the anonymous object you collide with? 1 Answer