Quantcast
Channel: Latest Questions by Griffo
Viewing all articles
Browse latest Browse all 94

Can I make an array of scripts?

$
0
0
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.

Viewing all articles
Browse latest Browse all 94

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>