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 /
avatar image
0
Question by Dakk · Nov 15, 2011 at 08:51 AM · c#javascriptvariable

How do i access a variable in one script from another script?

Hey, I am trying to access a variable in a script called HealthBarScript. HealthBarScript is attached to a gameObject called Player, whilst i want to access in a script called EnemyMovementScript wich is on a gameObject called Enemy. Here are the scripts.

Here is the HeathbarScript

 using UnityEngine;
 using System.Collections;
 
 
 public class HealthBarScript : MonoBehaviour {
     
     public int Maximumhealth = 100;
     public int CurrentHealth = 100;
     
     public float healthBarLength;
     
     // Use this for initialization
     void Start () {
         healthBarLength = Screen.width / 2;
     }
     
     // Update is called once per frame
     void Update () {
         AdjustCurrentHealth(0);
     }
     
     void OnGUI(){
         GUI.Box(new Rect(10, 10, healthBarLength, 20),
                 CurrentHealth + "/" + Maximumhealth);    
     }
     
     public void AdjustCurrentHealth(int adj){
         CurrentHealth += adj;
         
         if(CurrentHealth < 0){
             CurrentHealth = 0;    
         }
         
         if(CurrentHealth > 100)
         {
             CurrentHealth = Maximumhealth;    
         }
         
         if(Maximumhealth < 1){
             Maximumhealth = 1;    
         }
         
         healthBarLength = (Screen.width / 2) * (CurrentHealth / (float)Maximumhealth);
     }
 }





And here is the MovementScript

 var target : Transform; //the enemy's target

 var moveSpeed = 3; //move speed
 var rotationSpeed = 3; //speed of turning
 var myTransform : Transform; //current transform data of this enemy
 
 function Awake()
 
 {
 
     myTransform = transform; //cache transform data for easy access/preformance
 
 }
 
 
 
 function Start()
 
 {
 
     target = GameObject.FindWithTag("Player").transform; //target the player
 
 }
 
 
 
 function Update () {
 
     //rotate to look at the player
 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
 
 
     //move towards the player
 
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }
 
 function OnCollisionEnter(theCollision : Collision){
 
  if(theCollision.gameObject.name == "Player"){
 
  }else if(theCollision.gameObject.name == "Golv"){
 
       Debug.Log("Hit the wall");
  }
 }



I want to access CurrentHealth in the EnemyMovementScript at the bottom function called OnCollisionEnter. How would i do that?

What i want to happend is that i want them to collide and the health si withdrawn by like 5 or something.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Nov 15, 2011 at 12:04 PM

Like @syclamoth said, the problem here is the different languages: JS and CS can't see each other during compile time. There are a few ways to solve this, but since you need only a one-way communication path, I think the best alternative is to use SendMessage to call a function in the CS script that modifies the health.
In the CS script:

void AddToHealth(int value){ // add value to the health
    CurrentHealth += value;  // (or subtract, if the value is negative)
}
If both scripts are attached to different objects, you must have in the JS script a reference to the CS object - transform, gameObject, collider etc.
JS script:

var healthObject: Transform; // drag the health bar object here

function DecreaseHealth(){ // this function reduce health by 5 healthObject.SendMessage("AddToHealth", -5); } The other alternatives are:
1- Compiled scripts are visible to any language: if you compile JS after CS, the JS script will be able to "see" the CS one - but not the other way around, thus you may have problems with the JS script being invisible even to other JS code already compiled. The compilation order is explained in Script Compilation.
2- Rewrite the CS script in JS (or vice versa, if you have more CS than JS scripts). Maybe someday Unity will be able to compile JS, CS and Boo with the same compiler, but until that the best solution is to have all scripts written in the same language.

Comment
Add comment · 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
0

Answer by DaveA · Nov 15, 2011 at 08:56 AM

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Comment
Add comment · Show 2 · 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 syclamoth · Nov 15, 2011 at 09:25 AM 1
Share

This... isn't helpful. As far as I can tell, it's a problem with one being in C# and the other being in JS.

avatar image DaveA · Nov 16, 2011 at 04:59 AM 0
Share

In that case http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Lower player's health from separate script. 2 Answers

Multiplayer Problems 1 Answer

C# Declaring Variables Within If Statements 2 Answers

How to go about modifying a variable in a C# script from within a UnityScript (Javascript) that resides in a different Gameobject? 1 Answer

is Unity support socket javascpt client 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