t’s my code.it’s working fine but if I add more guns it should checks lots of ifs ,so I want to optimize it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour {
public int whatGun;
public void Drop(){
if(whatGun==0){
dropgun1 ();
}
else if(whatGun==1){
dropgun2 ();
}
else if(whatGun==2){
dropgun3 ();
}
else if(whatGun==3){
dropgun4 ();
}
}
public void dropgun1(){
gameObject.AddComponent<DropGun1> ();
}
public void dropgun2(){
gameObject.AddComponent<DropGun2> ();
}
public void dropgun3(){
gameObject.AddComponent<DropGun3> ();
}
public void dropgun4(){
gameObject.AddComponent<DropGun4> ();
}
}
``
i
how can I use functions as array? like this
public void Drop(){
dropgun[whatGun] ();
}
for example if whatGun=3 it runs
public void dropgun4(){
gameObject.AddComponent<DropGun4> ();
}
hope you guys get what I want ,any help other solution or whatever would be appreciated.
thanks for response,so I have 10 weapons with 10 completely different drop functions.let’s say I just have a script and have 10 drop function in this script.i just can use something like this to detect which gun is holing by the player
if(whatGun==0){
dropgun1();
}
else
.
.
.
I don’t really know how to use an array to run functions
like this
dropgun[whatGun]; //0 run dropgun1();,1 run dropgun2();
I hope you get what I want to do sorry for bad English.
I’m a little confused, You want to add a component to the gun to be able to drop it? and they’re all seperate components so 10 unneeded scripts? try generalise it to 1 script named “DropGun” and attach it to each gameObject.
Also, how do you recieve the “whatGun” integer, is it acuried from another script?
Are the guns stored somewhere? in a list/array? how do you switch between them?