- Home /
This question was
closed Dec 07, 2018 at 07:28 AM by
meikellp for the following reason:
The question is answered, right answer was accepted
Question by
meikellp · Apr 06, 2014 at 01:29 PM ·
javascriptvariablevariablesvar
Import Variables from another active script
I want to permanently access the "battery" var and "batteryMax" to create a "BatteryLifebar" How do I access this var without "GameObject.Find....."? flashlight.js:
var lightintensity : float = 100;
public var battery;
var time : float;
var time2 : float;
var flashingTime : float;
var batteries : int = 0;
public var batteryMax;
var Sound_Battery_PickUp : AudioClip;
var Sound_Flashlight_Toggle : AudioClip;
var Sound_Flashlight_Reload : AudioClip;
var batteries_text : GameObject;
function Start () {
battery = 100;
batteryMax = 100;
}
function PickUp (energy : int) {
batteries += energy;
audio.clip = Sound_Battery_PickUp;
audio.Play();
}
function Update () {
batteries_text.guiText.text = "Batterien: " + batteries + "\nLadung: " + battery + "/" + batteryMax;
if (Input.GetMouseButtonDown(2) && batteries > 0 && battery < batteryMax){
lightintensity += 0.83*20;
batteries -= 1;
battery += 50;
if (batteryMax < battery) {
battery = batteryMax;
lightintensity = batteryMax;
}
audio.clip = Sound_Flashlight_Reload;
audio.Play();
}
Screen.showCursor = false;
Screen.lockCursor = true;
if (light.enabled == true && battery > 0) {
time += Time.deltaTime;
if (time >= 1){
time = 0;
battery -= 1;
lightintensity -= 0.83;
}
if (battery <= 15 && battery > 0) {
time2 += Time.deltaTime*4;
if(time2 >= flashingTime) {
time2 = 0;
flashingTime = Random.Range(0.1, 1.2);
if (light.intensity == 0.25) {
light.intensity = 0.0;
}
else {
light.intensity = 0.25;
}
}
}
else {
if (battery > 0) {
light.intensity = lightintensity/100;
}
else {
light.intensity = 0;
lightintensity = 0;
}
}
}
if (battery <= 0) {
battery = 0;
light.intensity = 0;
}
if(Input.GetMouseButtonDown(1)){
if(light.enabled == true){
light.enabled=false;
}
else{
light.enabled=true;
}
audio.clip = Sound_Flashlight_Toggle;
audio.Play();
}
}
batterybar.js
var battery;
var batteryMax;
var batterybar : Texture2D;
var background : Texture2D;
function OnGUI () {
GUI.DrawTexture(Rect(0,-10,batteryMax,30),background);
GUI.BeginGroup(Rect(0,-10,battery,30));
GUI.DrawTexture(Rect(0,-10,batteryMax,30),batterybar);
GUI.EndGroup();
if(battery < 0){
battery = 0;
}
if(battery > batteryMax){
battery = batteryMax;
}
}
Comment
Best Answer
Answer by deltamish · Apr 06, 2014 at 01:34 PM
Thats really easy you could just reference the Object that contains the script or You could call FindGameObject at Start
var battery;
var batteryMax;
var batterybar : Texture2D;
var background : Texture2D;
public var BatteryScriptHolder:Transform;
private var BatteryScript:yourBatteryScriptName;//scripth that has the maxBattery var's
function Start()
{
BatteryScript = BatteryScriptHolder.GetComponent(yourBatteryScriptName);
}
function OnGUI()
{
battery = BatteryScript.battery;
batteryMax = BatteryScript.batteryMax;
// rest of your code
}
When I do this:
public var BatteryScriptHolder:Transform;
private var BatteryScript:flashlight;
function Start () {
BatteryScript = BatteryScriptHolder.GetComponent(flashlight) as (flashlight);
}
function OnGUI () {
battery = BatteryScript.battery;
battery$$anonymous$$ax = BatteryScript.battery$$anonymous$$ax;
}
The console says: "Batterybar.js(12,70): BCE0043: Unexpected token: as." and "Batterybar.js(12,72): UCE0001: ';' expected. Insert a semicolon at the end."
Converted answer by @meikellp to comment. Please use the comment button when replying to an answer.
hey would you $$anonymous$$d checking the tick mark to close this topic