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 /
This question was closed Jul 09, 2014 at 03:54 AM by robertbu for the following reason:

Other

avatar image
1
Question by Goldeneale · Jul 09, 2014 at 01:01 AM · c#classstruct

How -exactly- do classes and structs work in Unity?

Okay, so I read somewhere else on this site that when you attach a script to an object, as each script is a class, you are actually instantiating an object of that class. Is that correct?

So say I write a script like

 public class Test
 {
     public int x;
     public int y;
 }

If I add this script to a gameobject, does that instantiate an object of that class? The variables x and y would then show up in the Inspector, right? Is there any name for that object I can use to refer to it, or do I have to use GetComponent?

I've also heard that Unity automatically "includes" every script into every other script. Does that mean I could for example write one script which has several generally usable functions in it, and could then use those functions in other scripts? Say I have a class Math with a function Add, could I simply use Math.Add() in any other script? Do I have to put that script on a GameObject first?

What if I was for instance to make a class like

 public class Structure
 {
     struct Test
     {
         int x;
         int y;
     };
 }


If I put that script on a GameObject, will the struct Test be automatically instantiated, so I could use GetComponent on the script and then use Structure.Test.x? If not, how would I go about doing something like that?

Just some general questions I have.

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 robertbu · Jul 09, 2014 at 03:54 AM 4
Share

Unity Answers deals with single, specific technical issues concerning Unity. Your question has multiple technical issues, and largely has nothing to do with Unity. It deals with the ins and outs of C#. I highly encourage you to pickup any of the many beginning C# books and work through the chapters on classes. Here are some points to help direct your reading:

  • Classes derived from $$anonymous$$onoBehaviour are created by attaching the script to a game object. They must have a game object to exist. They are called components.

  • Instances of classes *not*derived from $$anonymous$$onoBehaviour must be created using the 'new' operator.

  • $$anonymous$$ost classes have instance data. For example, if you have a one AI script attached to multiple game objects, then each one may have a 'health' variable that is specific to that instance.

  • A function in a class that has the 'static' keyword in front of the function declaration can be called in the way you list above with '$$anonymous$$ath.Add()'. But these functions cannot reference instance data in the class. The functions in Unity's $$anonymous$$athf class are all static and therefore can be called like '$$anonymous$$athf.Sin()'. For other $$anonymous$$onoBehaviour-derived classes, you will need to get a reference to the instance of the object to change (Often using GetComponent()).

  • The biggest difference between classes and structs is that structs are by value, and classes are by reference. That is when you assign a struct to a new variable you get a copy of that variable. When you assign something that is an instance of a class, you assign a reference (and therefore the old and new variables are pointing at the same object.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Conversion of a static class to struct? 1 Answer

Constructor giving nullreference error 1 Answer

How to execute a Skript global? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

javascript call a method from c sharp and vise versa 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