So I’m working on my very first attempt at a game in Unity and I have a little problem.was hoping you could help.
I’m creating a little space shooters and I’ve decided that I want all my weapon firing functions to be in a different file with a separate class. Each weapon will have its own method and this way everything will be much more organised than it was before.
So I created a new file in Visual Studio, created a new class called Weapons, made it a subclass of Monobehaviour and set up a method called “public void Lasers(Vector3 PositionofShooter, GameObject LaserGameObject, int LevelofWeapon)”.
(As you can see I’ve decided to keep my GameObject for lasers inside the player code since “Weapons” will not be a attached to any object anyway… (Can I even do that? use a code not attached to an object?)
Now for my problem. Inside “Player”, how do I use the method “Lasers”? When I type in “weapons.” it suggest some methods that belong to monobehaviour but not my custom method. What the hell am I doing wrong here?
If you make Lasers a public static method then it will be accessible without an instance of Weapons which, from what it sounds like anyway, is what you’re trying to accomplish.
If you set it only to public variable you can use call GetComponent and access it. I would advise against static vars unless it requires only 1 instance of it. Like player’s health, bullets, name. Those variables can only have 1 instance.