Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Alphapieter1 · Feb 29, 2016 at 07:55 PM · c#classcommunication

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alphapieter1 · Feb 29, 2016 at 09:21 PM 0
Share

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

avatar image tonymotion · Sep 13, 2020 at 04:33 AM 0
Share

@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!

avatar image
0

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.

MSDN Interface documentation

If you have functionality shared between weapons, you can use a abstract Weapon class and partly code the functionality in the weapon base class.

MSDN Abstract documentation

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges