declare classes from another script c#
i am creating an hexbased strategie game. to save space all units are using the same script. evry unit has an mainweapon and secondaryweapon both are variables of the class weapon. All weapons(machinegun,rocketlauncher,howitzer...) are saved in the factorytilescript and from there i want them to assinged to the units the factorie produces as example and tank gets spanned and gets the tankgun as mainweapon and machinegun as secondary weapon assinged. but i have trouble declearing them as exactly same class as the wepons on the units are. So how can i declare the same class on two different scripts and more importent get my scripts to know that there are the same class
Answer by phil_me_up · Feb 29, 2016 at 08:24 PM
I'm not too sure what your asking. It sounds like you might be getting confused about classes though so here's a bit of general advice.
If you have a script called 'enemy.cs', then this one script can be applied to many components, you do not need multiple versions of enemy.cs to match the actual number of enemies you have. To customise the behaviour per enemy you declare public variables in the script and edit these per instance (i.e. for each gameobject your script is attached to). You can also use class inheritance which I recommend you look into.
Whilst it's possible to declare multiple classes in one cs file, this isn't really the way you work in Unity other than specific cases. If you want to have a Weapons and a Enemy class, make a weapon.cs and an enemy.cs file.
You mentioned that to save space, all the units use the same script. This doesn't really save space. It's certainly a good idea as it'll let you re-use code, but the size and number of scripts in a project probably isn't a concern until you work on much larger titles.
If, within a class such as enemy.cs you want to have a reference to a weapon, you would declare a variable like 'public Weapon m_primaryWeapon;'. You make it public so that within the editor, you can change it's value. In this case, you can drag a gameobject with the 'Weapon' script attached to it and then Enemy will be aware of Weapon. Alternatively you can create the weapon at runtime using Instantiate, and then assign that to the weapon variable.
There is a lot more to take into account, especially in that last paragraph but I hope it get's you started. If not, please edit your question or provide more detail so we can help.
script of my units
public class weapon
{
public bool isthere;
public bool isranged;
public bool[] canatk;
public int range;
public int attackdamage;
public int ammonition;
public int maxammonition;
}
public weapon mainweapon;
public weapon secondaryweapon;
and in the script of my factorie tile i want to do something like this:
public unitscript.weapon tankgun;
public unitscript.weapon mg;
then assigning some numbers to the variables
and once an unit like an tank gets spawned
newtank.getcomponent<unitscript>().mainweapon = tankgun;
newtank.getcomponent<unitscript>().sencondaryweapon = mg;
of course if there is an way this should also work the other way around with the declaretion of the class in the factorietilescript and the possibility to create variables of the class in the unitscript.
alos i tryed this exact sript and it didnt work
@phil_me_up, I've been searching for the past half hour or more trying to find out how to make a class written in one script to be available in another script, and your answer was the first and only one to do it (at least in a way that made any sense). Thank you!
Answer by RobGraat · Feb 29, 2016 at 08:41 PM
I think you need to read up on interfaces and abstract classes.
Use Interfaces if weapons share the same method names but each having a different implementation.
If you have functionality shared between weapons, you can use a abstract Weapon class and partly code the functionality in the weapon base class.
Make the different weapons derive from a abstract WeaponBase class. And have the units reference Primary and Secondary weapons of the WeaponBase type.
Cheers, Rob
Your answer
![](https://koobas.hobune.stream/wayback/20220612070102im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is this true? 1 Answer
C# script class bug 0 Answers
Changing an argument to a var and vice-versa 0 Answers
Programmmed Animation (making transform move to vectors with code) 0 Answers
Nested for loop isnt working 1 Answer