Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by SmooveB · Jun 16, 2012 at 12:37 AM · c#classesbetween

Using classes between scripts in C#?

In UnityScript, if I had a class named Zone in the script named Uni, and I wanted to create an instance in other script, I could just say

var oneZone : Zone.

In C# I have to use the name of the script eg

private Uni.Zone oneZone.

I don't understand why this is and I'm worried that I am screwing something up because I don't see this mentioned anywhere in any Google search.

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
2
Best Answer

Answer by whydoidoit · Jun 16, 2012 at 12:40 AM

If you don't define your Class inside your MonoBehaviour - just put it outside - then you can just create it like the US version.

US is basically fiddling to generate a class for your script behind the scenes, and then it's guessing you didn't want to embed the extra class when you write one in the file. (You can make it embed it by explicitly defining your behaviour class as well if you really want to).

So just have multiple classes, don't embed one inside the other if you don't want to reference it that way.

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 whydoidoit · Jun 16, 2012 at 12:42 AM 0
Share

For clarity:

SomeScript.cs

  public class SomeScript : $$anonymous$$onoBehaviour {
  ...
  }

  public class $$anonymous$$yExtraClass {
  ...
  }
avatar image SmooveB · Jun 16, 2012 at 01:28 AM 0
Share

Thanks. It's obvious now that you point it out. I was not thinking of my scripts as classes because (even though they are) in US it is not explicit so I got confused.

avatar image
2
Wiki

Answer by Berenger · Jun 16, 2012 at 12:53 AM

It depends of the scope where the class is declared. You have the namespace then the class (might need confirmation on that).

Example 1 : The class is a tool used internally by main class and nobody else need it.

 public class MainClass
 {
     private Tool tool; // A higher accessibility would raise an error here
     
     public void DoStuff(){ /* stuff where tool is used */ }
 
     // This class won't be used by anyone else, we can make it private.
     // It could be protected if you wish to inherit from MainClass.
     private class Tool{ /* stuffs */ }
 }

Example 2 : The Tool class is needed to use the MainClass, but only in that case. It is its only purpose, so you can declare it inside the main class.

 public class MainClass
 {
     // Notice that the public function take a Tool in parameter.
     // This mean whoever call it needs access to Tool
     public void DoStuff(Tool tool){ /* stuff where tool is used */ }
 
     // That's why you make it public.
     // To instantiate a tool object, you'll call "new MainClass.Tool()"
     public class Tool{ /* stuffs */ }
 }

Example 3 : The tool class can be used in a lot of situations, not only with the main class. You can then declare it outside. Think Vector3, for instance.

 // By declaring it outside of MainClass, you can
 // use at a usual class (Tool t = new Tool();)
 public class Tool{ /* stuffs */ }
 
 public class MainClass
 {
     public void DoStuff(Tool tool){ /* stuff where tool is used */ }
 }


PS : If I made any mistake, which is fairly possible, feel free to edit me.

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 whydoidoit · Jun 16, 2012 at 12:59 AM 0
Share

Looks damn good to me - great explanation in less than 1 page :D

avatar image SmooveB · Jun 16, 2012 at 01:26 AM 0
Share

Thanks. Great answer. I really knew this, but I think using US got me thinking in the wrong direction. I could not even see the class declaration staring me in the face at the top of all of my scripts.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Scripts don't work in Unity 3.5 any idea why? 1 Answer

WWW No valid crossdomain policy available to allow access 0 Answers

Enum with multiple fields 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