Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Algoprism · Jul 27, 2021 at 09:02 AM · classobjectsfilessplitting

Splitting with Partial Classes

Hello!

I am in the process of making an application that involves putting scripts onto GameObjects and I have entered a small roadblock. Could really do with some advice seeing I'm new to Unity and C#.

Being a student that has used C++, I am used to splitting up my content across multiple files and I would like to do the same within Unity.


The problem:

When searching online, the result I was directed towards was using partial classes to split my files, which if I'm not mistaken, requires you to have the same name class name in order to work - But what if my files are called different things?

 public partial class MainStuff: MonoBehaviour //In a file called "Mainstuff"
 {
     public GameObject Handle; //This variable is required by initialise Object, and needs to stay in this class.

     public void initialiseObject()
     {
         //Script stuff here. Calling the Handle GameObject.
     }
 }
 
 
 public partial class Mainstuff: MonoBehaviour //In another file called "ObjectStuff"
 {
     //Where I would like to move initialiseObject()
 }

I called both the classes MeshFunctions in order to prevent errors, but when I try apply the "ObjectStuff" file to the GameObject, It returns with: "Can't add script component 'ObjectStuff' because the script class cannot be found.


"But you need to rename Meshfunctions to the same name as ObjectStuff"


If I do so, then the class in ObjectStuff is unable to see the Gameobject Handle. Because I'm guessing that's how partial classes work. I'm usually used to things like headers that you can put your variables into.

Any pointers towards this would be brilliant thank you!

Comment
Add comment · Show 1
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 CodesCove · Jul 27, 2021 at 07:26 PM 0
Share

At least one of the files needs to match the class name and this file is the one you can attach to GameObjects. The whole class (the including the code in the other files) is serialized and shown in the Inspector (even if you cannot attach those other files to GO's) and you can see the partial classes all public fields there.

So, you cannot attach the "ObjectStuff" file to the GameObject, only the "Mainstuff", but should be enough

Of course you need to make sure the files are in the same namespace and assembly.

0 Replies

· Add your reply
  • Sort: 

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

126 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 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

Warning on creating an array of class objects 3 Answers

Using accessors and mutators from another class 2 Answers

Add all objects of a type to a list before runtime 1 Answer

Why using this way class attributes are omitted? 0 Answers

What are GameTiles? 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