Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • 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

avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
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
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meikellp · Apr 06, 2014 at 01:48 PM 0
Share

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."

avatar image tw1st3d · Apr 06, 2014 at 01:49 PM 0
Share

Converted answer by @meikellp to comment. Please use the comment button when replying to an answer.

avatar image deltamish · Apr 06, 2014 at 02:12 PM 0
Share

sorry edited it

avatar image meikellp · Apr 06, 2014 at 02:39 PM 0
Share

Everythings fine, ive fixed it!

avatar image deltamish · Apr 06, 2014 at 07:34 PM 0
Share

hey would you $$anonymous$$d checking the tick mark to close this topic

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

One of my scripts can't find the variable from this script 1 Answer

Adding varibales from other scripts 3 Answers

var TheCollision : Collision 1 Answer

Which variable can be implicitly declared? 1 Answer

Is it possible to create new variables by incrementing? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges