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 Griffo · Feb 21, 2014 at 12:48 PM · arrayarrays

Can I make an array of scripts?

Can I make an array of scripts?

I have a global variable script (GlobalVars.js) that hold all my variables for my game, two of them being the currentWeapon and the other currentWeaponNumber that holds the current weapon being used and the number of the weapons position as the child of an empty game object called weapons, each weapon has a script called AnimationCtrlWeapon.js on it

 public var currentWeapon : Transform;
 public var currentWeaponNumber : int;

I've made an array of scripts (the way I thought it should be done) in the GlobalVars.js like below.

 public var weaponScriptArray : Array = new Array(7);
 
 public var Bazooka : GameObject;
 public var G36c : GameObject;
 public var HandGun : GameObject;
 public var M16Scope : GameObject;
 public var PPSH41 : GameObject;
 public var ShotGun : GameObject;
 public var Grenade : GameObject;
 
 function Start(){
     
 // Find all the weapons
     Bazooka = GameObject.Find("Bazooka");
     G36c = GameObject.Find("G36c");
     HandGun = GameObject.Find("Hand Gun");
     M16Scope = GameObject.Find("M16 Scope");
     PPSH41 = GameObject.Find("PPSH41 Gun");
     ShotGun = GameObject.Find("ShotGun Without Light");
     Grenade = GameObject.Find("Stick Grenade");
 
 // Populate weaponScriptArray with the weapons AnimationCtrlWeapon script
     weaponScriptArray[0] = Bazooka.GetComponent(AnimationCtrlWeapon);
     weaponScriptArray[1] = G36c.GetComponent(AnimationCtrlWeapon);
     weaponScriptArray[2] = HandGun.GetComponent(AnimationCtrlWeapon);
     weaponScriptArray[3] = M16Scope.GetComponent(AnimationCtrlWeapon);
     weaponScriptArray[4] = PPSH41.GetComponent(AnimationCtrlWeapon);
     weaponScriptArray[5] = ShotGun.GetComponent(AnimationCtrlWeapon);
     weaponScriptArray[6] = Grenade.GetComponent(AnimationCtrlWeapon);
 }

Then in a script called button.js I tried to access the current weapons AnimationCtrlWeapon.js

In buttons.js I have

 private var globalVars : GameObject;
 private var globalVarsScript : GlobalVars;
 
 function Start(){
     globalVars = GameObject.Find("Global Vars");
     globalVarsScript = globalVars.GetComponent(GlobalVars);
 }

So I thought to alter say the bulletCount variable in the current weapons AnimationCtrlWeapon.js I would use

 globalVarsScript.weaponScriptArray[globalVarsScript.currentWeaponNumber].bulletCount = 6;

But I get the error message - 'bulletCount' is not a member of 'Object'.

I can access the variable with

 globalVarsScript.currentWeapon.GetComponent(AnimationCtrlWeapon).bulletCount = 6;

But was trying to miss out GetComponent()

Anyone know what wrong please? Is it because the weaponScriptArray has to be set as type : Script? Thank you.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by KiraSensei · Feb 21, 2014 at 12:54 PM

weaponScriptArray Should be an array of AnimationCtrlWeapon.

Try something like :

 var weaponScriptArray : List.<AnimationCtrlWeapon>;
Comment
Add comment · Show 1 · 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 Griffo · Feb 21, 2014 at 01:06 PM 0
Share

That my friend worked perfectly .. Thank you.

avatar image
1

Answer by Linus · Feb 21, 2014 at 01:07 PM

This works quite well for me. You even get to see the list in the inspector.

 var uiLabels : List.<UILabel> = new List.<UILabel>();
   
 function Start(){
     var labels : UILabel[] = GameObject.FindObjectsOfType(UILabel) as UILabel[];
     for (var label : UILabel in labels) {
         uiLabels.Add(label);
     }
 }
Comment
Add comment · Show 1 · 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 Griffo · Feb 21, 2014 at 01:19 PM 0
Share

Thank you.

avatar image
0

Answer by omerselman · Apr 26, 2017 at 01:47 PM

3 years late but for people who need this; This is how i add scripts to an array or list and with one script i can disable or enable them... hit like if you like :)

 public MonoBehaviour[] disabledOnes;

 public void Update(){
 if (disabledOnes.Length > 0)
     {
         foreach (MonoBehaviour disableThis in disabledOnes)
         {
             disableThis.enabled = false;
         }
     }

}

Comment
Add comment · Show 3 · 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 siempus · Nov 29, 2017 at 02:30 AM 0
Share

I don't know why u got dislike. but you're the only one who answer the question. thanks.

avatar image omerselman siempus · Nov 30, 2017 at 12:15 PM 0
Share

I dont know as well :))

avatar image liveswithoutfear · Aug 07, 2021 at 09:17 AM 0
Share

Dude you rock! And another 4 years later and you're still helping people out. I kept trying to use strings, GetComponents and var... Couldn't ever !enable a var haha. Basically overcomplicating things. This worked perfectly... Literally 2 lines of code...

 public MonoBehaviour script;

 void Start()
 {
     script.enabled = false;
 }

Just so simple.

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

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

how to build & populate an array available to all scripts on all objects? 1 Answer

How to store aspects (position, name, tag) of a game object in an array to be used for reinstantiation? 1 Answer

How to return index of List element? 1 Answer

How many audio clip arrays are supported? 1 Answer

Get all GameObjects by variable value 2 Answers


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