- Home /
Get other GameObject's script name
Hello everyone,
I'm trying to create a system in which you can swap different guns, each one with a different script, so I need a way to access the methods in each script depending on the weapon the character is using from the character's script, but I can't find a suitable way to do it.
Thanks
Answer by mchts · Apr 12, 2019 at 09:01 AM
Basic oop rules inheritance and polymoprhism. Create a gun class and create Fire(), Reload() vs. methods inside it. Than derive your each gun from that base class and override all of your methods in each gun script depending on your gun type. So you could call just one method but each gun will react different.
Answer by xxmariofer · Apr 12, 2019 at 08:56 AM
you can have a parent class for all the classes something like this
PARENT
public class Weapon : MonoBehaviour {
}
Childs
public class Weapon1 : Weapon {
}
public class Weapon2 : Weapon {
}
and access the type of your weapon
Debug.Log(yourWeapon.GetComponent<Weapon>().GetType().Name);//this will give your the name of the clas