Hello,
I have a first script ‘Player’ which instantiate a gameObject name “Gun” with a script within and add it like child. In my Gun script, i declare all variables about what kind of gun it is, rate of fire,ect…
I create a prefab ‘Player’ and put 2 of them on my scene. Every players must have his ‘Gun’. I cannot use ‘static’ for my var Gun’s but i need to use them on my ‘Player’ script. How can i do?
You need a variable on your PlayerScript that references the gun. (public GunScript gun).
When the gun is instanced, the GunScript could insert itself into the PlayerScript’s gun variable. You can find the gun’s parent with “transform.parent” or “transform.root” then do ‘transform.parent.GetComponent().gun = this’.
Or you can do it the opposite way, where the PlayerScript will search its children to find the gun.
Okey thanks !! I will try this. In fact my problem was i make this : ‘public class Weapon : Gun{}’ 'cause my Player can has other weapon than Gun like a stuff in rpg. So it is a good way to use class on my Player or Gun or not?
I think i will have some problems when my project grow up.
I think you have those backwards. You want Gun to inherit from weapon, not the other way around. So you’ll have one script:
public class Weapon : MonoBehaviour { }
And then:
public class Gun : Weapon { }
Ok. I think i will look some tuto about how organize a project ! Thanks a lot