use functions as array???

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.

Use code tags in your posts .

1 Like

Consider using switch statements, or just having a single dropgun function that takes a gun value.

Basic switch example, and passing a value to a function:

public void Drop(int gunToDrop)
{
    switch (gunToDrop)
    {
        case 1:
            dropgun1();
            break;
        case 2:
            dropgun2();
            break;
        case 3:
            dropgun3();
            break;
        default:
            Debug.Log("Oops, shouldn't get here");
            break;
    }
}

public void ExampleCallingFunction()
{
    Drop(2);  //drop gun # 2
}

I’d also suggest not having separate DropGun# components. Have a single DropGun component and then customize its settings after you add it.

gameObject.AddComponent<DropGun>();
GetComponent<DropGun>().Initialize(2);  //Initialize the DropGun component as a gun #2
1 Like

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. :slight_smile:

I’ve never used an array to run functions (is that even possible?). I’d just use a switch to run functions.

1 Like

hmmm I did it too ,but if it was possible it was very nice :slight_smile: thanks for response.

On the topic of whether it’s possible, sure you could have an array of delegates.

can you show me an example please?

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?

1 Like

it’s not only about dropping a gun or whatever ,I got the answer in this post.thanks for response :slight_smile:

https://forum.unity.com/threads/change-script-name-between.515957/#post-3382881

it’s very nice and easy and it’s exactly what I wanted :slight_smile:

Learn enums. You don’t want to go
== 0
== 1

etc. That’s terrible. Learn enums (they can be cast to a number anyway if you really need to). Will greatly enhance code legibility in this case.

https://forum.unity.com/threads/change-script-name-between.515957/#post-3382881

I’m a little lazy :slight_smile: . it solved the problem thanks buddy :slight_smile:

I think the answer you got was probably better than creating an array/list of functions, anyways - for your use case.

For the record, though – learning the available options in programming is very helpful. Then you can pick the best option for the task.

1 Like