Hi,
I am making a Sidescroller-Shooter Game and im at the point, where i want to add multiple Weapons to my player, that I can switch through. So I used a Generic list to this, which has the variables, that need to be assigned in it.
public class Weapons
{
public var name : String = "Weapon";
public var firemode : fMode = fMode.semi;
public var firerate : float = 0.1f;
public var model : Transform;
public var raystart : Transform;
public var muzzle : GameObject;
public var muzzlelight : Light;
}
I Better show you the full script, so you can see it better.
#pragma strict
import System.Collections.Generic;
private var muzzletime : float = 0.1;
public var CurrentWeapon : Weapons; //Here i got the List defined
public var weapons : List.<Weapons>; //CurrentWeapon is for the Weapon in hand
weapons = new List.<Weapons>();
// Use this for initialization
function Start () {
weapons.Capacity = 2;
Debug.Log("möglich" + weapons.Capacity + ", drin" + weapons.Count);
CurrentWeapon.muzzlelight.light.enabled = true;
CurrentWeapon = weapons[0]; //here i choose a list entry || It says the Mistake is here
}
// Update is called once per frame
function Update () {
Muzzlecontroll();
}
function Shoot (i : int) {
if(Input.GetMouseButtonDown(i) && CurrentWeapon.firemode == fMode.semi){
if(Physics.Raycast(CurrentWeapon.raystart.position, CurrentWeapon.raystart.TransformDirection(Vector3.forward), 20)){
Debug.DrawRay(CurrentWeapon.raystart.position,CurrentWeapon.raystart.TransformDirection(Vector3.forward) * 10,Color.red);
Debug.Log ("Something was hit");
}
CurrentWeapon.model.animation.Play("Recoil");
CurrentWeapon.muzzle.renderer.enabled = true;
muzzletime = 0.1;
CurrentWeapon.muzzlelight.light.enabled = true;
}
if(muzzletime < 0){
CurrentWeapon.muzzle.renderer.enabled = false;
}
}
function Muzzlecontroll () {
if(muzzletime >= 0.0){
muzzletime -= Time.deltaTime;
}
if(muzzletime < 0){
CurrentWeapon.muzzle.renderer.enabled = false;
CurrentWeapon.muzzlelight.light.enabled = false;
}
}
public enum fMode
{
rocketlauncher,
auto,
semi,
bolt
}
public class Weapons //<--- The weapon class
{
public var name : String = "Weapon";
public var firemode : fMode = fMode.semi;
public var firerate : float = 0.1f;
public var model : Transform;
public var raystart : Transform;
public var muzzle : GameObject;
public var muzzlelight : Light;
}
Then i fill in the variables in the editor.
But if i then want to start the game, it sets my size of the list again to 0,
and says: “ArguementOutOfRangeException: Argument is out of Range”
And says the Mistake is thereCurrentWeapon = weapons[0];
Hopy someone could help me and that my Question is readable.
I really need some help.