Hi. So i was trying to change grenades that i throw when i press "h".But how can i do it , i mean how can i change Bullet Prefab. I have my grenade throwing script here. Code (csharp): public var BulletPrefab : Transform; public var BulletSpeed : float = 2500; function Update (){ if(Input.GetKeyDown ("h")) { //BulletPrefab change to Grenade1 //If input h again change to Grenade2 //or again change to Grenade3 } } function Throw () { Gammo -= 1; yield WaitForSeconds (0.1); var bulletCreate = Instantiate (BulletPrefab, GameObject.Find("SpawnPoint").transform.position, GameObject.Find("SpawnPoint").transform.rotation ); bulletCreate.rigidbody.AddForce(transform.forward * BulletSpeed); } Should i do something like Code (csharp): var Grenade 1 : Transform; var Grenade 2 : Transform; var Grenade 3 : Transform; //.......... if(Input.GetKeyDown ("h")) { BulletPrefab = Grenade 1; //and what next } Someone knows how to do this?Or what should i do? Thank you -3DK
Just cycle through a list or array of grenade prefabs. For example (in c# sorry, I dont do UnityScript) Code (csharp): public List<Transform> m_grenadePrefabs; // put your grenade prefabs in here public int m_currentGrenadeIndex = 0; // index of currently selected grenade void Update() { if(Input.GetKeyDown ("h")) { ++m_currentGrenadeIndex; if(m_currentGrenadeIndex >= m_grenadePrefabs.Count) { m_currentGrenadeIndex = 0; } } } void Throw() { BulletPrefab = m_grenadePrefabs[m_currentGrenadeIndex]; ............. }
Thanks for help, but can someone explain me in java what should i do, because i don't know C# Thanks again.
Both c# and unityScript support lists and arrays so the concept is exactly the same in either language, its just the syntax that changes slightly. If you're not already familiar with arrays or lists I suggest you look up some tutorials on the subject since they are extremely useful.
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use? a little over half way down it has the c# and "javascript" (aka unityscript) equivalent for lists.
Nullstar and Lefty if you or anybody gave me the script that i needed i wouldn't learn anything , but since you didn't i'm glade because i managed to understand it , and write it my self.So i wanted to thank you Here's the code if any body needs it Code (csharp): var BombPrefab : GameObject[]; private var BombNum : int = 0; private var BombCount : int = 2; public var ThrowFoce : float = 2500; var Animation : AnimationClip; var Gammo : int = 5; //Hide/show objects var Hands : GameObject; var Weapons : GameObject; function Update () { if(Input.GetKeyDown ("h")){ BombNum++; if(BombNum == BombCount) { BombNum = 0; } } if(Input.GetKeyDown ("g")) { if(Gammo > 0){ Hands.animation.Play (Animation.name); Hands.animation[Animation.name].speed = 1.4; Throw(); Enable(); Weapons.active = false; Hands.active = true; } } if(!Hands.animation.IsPlaying(Animation.name)) { Weapons.active = true; Hands.active = false; } } function Enable () { if(!Hands.animation.IsPlaying(Animation.name)) { Weapons.active = true; Hands.active = false; } } function Throw () { Gammo -= 1; yield WaitForSeconds (0.1); var BombCreate = Instantiate (BombPrefab[BombNum], GameObject.Find("SpawnPoint").transform.position, GameObject.Find("SpawnPoint").transform.rotation ); BombCreate.rigidbody.AddForce(transform.forward * ThrowFoce); }