• 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
Question by Griffo · Jun 22, 2013 at 07:24 PM · variablegetcomponent

How would I write this?

Hi, say i have a script called "GlobalVars.js" with global variables in it like -

 // --------------- ENEMY TYPES -----------------
 public var enemyType : int[] = new int[10];        // The Type of Enemy .. Type1, Type2 .. ect up to a maximum of 10 Types
 // ---------------------------------------------
 
 // --------------- ENEMY VARIABLES -----------------
 public var DistanceToPlayer : float[] = new float[10];                            // Distance to the player
 public var MusicPlaying : boolean[] = new boolean[10];                            // Is the attack music playing for this Enemy
 public var PlayerAngle : float[] = new float[10];                                // The angle between the Enemy and the Player
 public var HitByRPG : boolean[] = new boolean[10];                                // Is the Enemy taking a hit by a RPG
 public var HitByGrenade : boolean[] = new boolean[10];                            // Is the Enemy taking a hit by a Grenade
 public var HitGrenadeRPGCount : int[] = new int[10];                            // How many times hit by a Grenade or RPG
 public var numberOfTheEnemyTypes : int;                                            // Enemy1, Enemy2, Enemy3, ect ...
 public var Health : float[] = new float[10];                                     // Set an array of the Enemy's Health
 public var Alive = Enumerable.Range(0,10).Select(function(c) true).ToArray();    // Is the Enemy alive
 public var Shooting : boolean[] = new boolean[10];                                // Is the Enemy shooting set to false
 public var BeingShot : boolean[] = new boolean[10];                                // Is the Enemy being shot
 public var TypesBonus : int[] = new int[10];                                    // Bonus points for killing a Enemy
 public var killigEnemyHealthBonus : int[] = new int[10];                        // Bonus health for killing a Enemy

And I have 4 enemy 2 named -

"Enemy Type 1" with tags of "Enemy 1" and "Enemy 2"

and 2 named -

"Enemy Type 2" with tags of "Enemy 1" and "Enemy 2"

each with the same scrip attached, how would I set a variable from their script, say I want to set the Health variable on the "GlobalVars.js" scrip of "Enemy Type 1" with tag of "Enemy 2"

This is how I thought I would do it -

 #pragma strict
 
 import System.Linq; // This is needed for the "function ConvertToInt" and "ConvertToInt(transform.tag)" to work
 
 private var globalVars : GameObject;    // The GameObject with the GlobalVars.js containing all the Variables for the game
 private var enemyTagNumber : int;
 private var enemyTypeNumber : int;
 private var enemyHealth : float = 100;
 
 function Start () {
 
     enemyTagNumber = ConvertToInt(transform.tag);    // Get the Enemy Tag number from it's tag
     enemyTypeNumber = ConvertToInt(transform.name);    // Get the Enemy Type number from it's name
     globalVars = GameObject.Find("Global Vars");
 
 // **** THIS IS HOW I THOUGHT IT WHOULD BE DONE ****
     globalVars.GetComponent(GlobalVars).enemyType[enemyTypeNumber].Health[enemyTagNumber] = enemyHealth;
 }
 
 // ---------------------------------------------------------------
 // Function to extract the numer from the EnemyX.tag or .name
 // ---------------------------------------------------------------
 function ConvertToInt(stringContainingNumber : String) : int{
    return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit(c))));
 }
 // ---------------------------------------------------------------

But it says 'Health' is not a member of 'int'. any help would be great, thanks.

Comment

People who like this

0 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 AlucardJay · Jun 23, 2013 at 12:57 AM 0
Share

Dude, your question titles are getting worse and worse ....

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by GreenBeanDemon · Jun 23, 2013 at 03:34 AM

What I have seen posted for this sort of thing is to make a var and do a GetComponent look up as little as possible. As I am guessing your doing more than one look up to get the stuff you wanted.

 var globalVarsScript : GlobalVars; // var name : script name as the Type
 gobalVarsScript = golbalVars.GetComponent(GlobalVars);
 
 
 gobalVarScript.enemyType[enemyNumber] = enemyTypeNumber;
 gobalVarScript.Health[enemyNumber] = enemyHealth;
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

script with individual function for each gameObject or public script variable to solve inventory system problem 1 Answer

Passing a Script Name to a Function 2 Answers

How to set a variable equal to another variable in another gameobject? 2 Answers

Why can't I change another script's variable with this script? Thanks, 1 Answer

How to call a function on an instance 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