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 Hi_No_Te · Nov 21, 2012 at 07:48 PM · getcomponentvariablesobjectsother

Accessing Scripts and Their Variables from other Objects

I am trying to look at a float from a Script that is contained in another object (or on a few objects). The Character Controller I set up has this to read a variable (HealthRatio) to use to show to the player how much health (via healthbar) the enemy has.

if(Tag == "Enemy") { EnemyStats = hit.collider.gameObject.GetComponent("EnemyScript"); EnemyHealth = EnemyStats.HealthRatio; EnemyHealthBarLength = BarLength * (EnemyHealth); }

However, Unity declares that the "*" cannot be on the left handside of the Object "EnemyHealth." It seems that EnemyHealth isn't set as avariable, but as a GameObject. I tried using GetComponent, but obviously it did not work as I desired.

Thank you for the help.

Comment
Add comment · Show 1
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 sparkzbarca · Nov 21, 2012 at 07:56 PM 0
Share

format code with the 10101 button post EnemyScript and post where BarLength is decalred and set.

It APPEARS the problem is the game has no idea how to multiply those 2 objects.

The problem for helping you is this is JavaScript not C# so I can't see what type an object is without seeing the declaration.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by PAEvenson · Nov 21, 2012 at 07:58 PM

you need to typecast the component to the variable you want it to be `hit.collider.gameObject.GetComponent("EnemyScript") as EnemyScript;`

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 Hi_No_Te · Nov 21, 2012 at 11:27 PM 0
Share

That by itself did not fix the problem. Thanks though

avatar image sparkzbarca · Nov 21, 2012 at 11:28 PM 0
Share

it finds out variable types by itself and force casting shouldnt normally be done unless you know its safe just post some code up man so we can see whats wrong.

Your saying a * b doesn't work wihtout showing a or b

add in formatting as well.

avatar image
0

Answer by Hi_No_Te · Nov 21, 2012 at 11:37 PM

Here's the posting of the area that's giving me trouble and BarLenght.

         if(Tag == "Enemy") {
         
         EnemyStats = hit.collider.gameObject.GetComponent("EnemyScript") as EnemyScript;
         EnemyHealth = EnemyStats.HealthRatio;
         
         EnemyHealthBarLength = BarLength*(EnemyHealth);    

Here's BarLenght's declaration:

 var BarLength : float = 380;


And EnemyScript:

 var Health : float;
 var Stamina : float;
 var Mana : float;
 
 var CurrentHealth : float;
 var CurrentStamina : float;
 var CurrentMana : float;
 
 static var HealthRatio : float;
 static var ManaRatio : float;
 static var StaminaRation : float;
 
 function Start () {
 
     if(gameObject.name == "Capsule") {
     
     Health = 120;
     Mana = 120;
     Stamina = 120;
     
     CurrentHealth = 100;
     CurrentMana = 120;
     CurrentStamina = 120;
     
     HealthRatio = CurrentHealth/Health;
     ManaRatio = CurrentMana/Mana;
     StaminaRation = CurrentStamina/Stamina;
     
     }
 }
 
 function Update () {    
 
 }
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 Hi_No_Te · Nov 22, 2012 at 06:56 PM

I figured it out, and I did not post it:

     private var EnemyHealth : float;
     private var EnemyMana : float;
     private var EnemyStamina : float;

I did not add the ": float: and it took each as an object. As soon as I replaced it, it worked.

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

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

11 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

Related Questions

How do I add/subtract variables between two different JavaScripts? 1 Answer

Using a variable value with GetComponent 1 Answer

Need help with this please. 1 Answer

Calling variable accross scripts in C# problem (Closed) 1 Answer

Why i can not access these variables? 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