- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Sketch_Kidd · Mar 03, 2014 at 11:48 PM ·
floatvaluesaccessing scripts
Changing gameobjects Float from another script?
Ive been trying to change the float in my player objects script,
from another object shooting a raycast, but every time the player
makes contact with the raycast the target value doesnt change. So I added Debug log to show current health float on my enemies
script, the value does decrease but it instantly snaps back to the
default value. I'm relitively new to coding and I would prefer using C#, some how
its esier for me to understand than Unity's javascript.
Heres my script for ememy..
using UnityEngine;
using System.Collections;
public class enemy_detect : MonoBehaviour {
public float scensorreach = 3f;
public float damage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject controller = GameObject.Find ("Player");
Health HP = controller.GetComponent <Health> ();
float currenthealth = HP.health;
Vector3 scensorrenderFwd = transform.TransformDirection(Vector3.right) * scensorreach;
Debug.DrawRay (transform.position, scensorrenderFwd, Color.red);
///___________________________________///
Vector3 scensordetectfwd = transform.TransformDirection (Vector3.right);
bool scensorfwd = Physics.Raycast (transform.position, scensordetectfwd, scensorreach);
if (scensorfwd) {
currenthealth += -1;
if (currenthealth < 100)
damage += 1 * Time.deltaTime;
if (currenthealth == 0)
Destroy (controller);
}
Debug.Log (damage);
Debug.Log (currenthealth);
}
}
And heres the player script with the health value Im trying to change,
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour{
public float health = 100;
private float Width = Screen.width;
private float Height = Screen.height;
void Start() {
}
void Update() {
}
void OnGUI() {
GUI.Box(new Rect(Width/2 - health * 2, 10, health * 4,
20),"HP " + health);
}
}
does anyone have any idea whats wrong with the script
Comment
Answer by sheffieldlad · Mar 04, 2014 at 01:09 AM
In your 1st script change Hp.health rather than currenthealth