How do i make it so it cycles trough a list on input?
so i want to make it so when i input, lets say for example "F" it cycles trough a list of either, scripts or functions, cause i want it so i can change weapons on mouse wheel up and down, and so i can change burst type when F is pressed.
Answer by UnityCoach · Apr 11, 2017 at 10:04 PM
You could do something like this :
List<Weapon> weapons; // a list of weapon components
public Weapon weapon // an accessor to get the current weapon
{
get { return weapons[index]; } // that returns a weapon from the list using an index
}
private int _index;
public int index // an index accessor
{
get { return _index; }
set
{
_index = value > weapons.Count-1 ? 0 : value; // a ternary operator to cycle through
}
}
void Update ()
{
if (Input.GetKeyDown (Keycode.F)
index ++;
}
Answer by Kemorno · Apr 12, 2017 at 05:59 AM
thank you very much, new to coding, so i dont really know that much about it...
You may be interested to have a look at my youtube channel then. You can also have a look at my complete online training. Actually, switchable weapons is part of it :)
Your answer
Follow this Question
Related Questions
How to find all AudioClips in Resources folder and play them? 1 Answer
Referencing instances of scripts based on the GameObject they are attached to. 0 Answers
Class structure for diferent item types and items that do different things. 0 Answers
Please Help Me: Assets/Scripts/PlayerControaller.cs(34,33): error CS8025: Parsing error 1 Answer
Rigidbody changes reference in play time to the game object that the script is attached to 1 Answer