• 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 digs · May 02, 2014 at 10:54 AM · type

Trying to understand "types" like GameObject, etc

Hello - I'm a beginner, if there's a beginners page please point me in that direction!

I've been examining/dabbling in code for years; I understand why 3 is of type int, why "cup" is a string, and why 'difficulty myDifficulty' inherits enum when 'enum difficulty;'

but one thing that I've never really understood, especially now with c# in unity, is

what is the type 'GameObject' - what happens when I assign a variable type GameObject... ie, GameObject cube;

another example

You write a class called 'Orientation' and assign it to a cube, it creates rotation for z axis, then in another class you refer to it by creating a variable of type 'Orientation', ie, Orientation spinAmount;

so now spinAmount is type Orientation.... my problem is that I don't understand how 'Orientation' is a type.

What is the purpose of having these (to me) obscure types? Can you make anything a type? I always assumed 'types' were like int - a number (and absolute -or- predefined)

Are there things that can't be 'types'?

Hopefully that made sense and someone else has had this same feeling? Thanks for any insight

Comment
Add comment · Show 2
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 hamstar · May 02, 2014 at 02:28 PM 0
Share

It sounds like you are not very familiar with OOP. You may find this tutorial useful, or google for others - "intro to oop".

avatar image digs · May 03, 2014 at 05:58 PM 0
Share

Thanks a lot guys, this is making much more sense now! Especially after I created a public Rigidbody type, and saw that the inspector wanted me to drag a rigid body in there! Unity is pretty snazzy

When must you make something 'new'

I see that often, but am not sure why you need to make a 'new Vector3', for example

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · May 02, 2014 at 02:06 PM

Read any programming book, or web page(?) about making and using classes (or structs or any datatype.) It's a really nice trick that we invented long before Unity or C#.

Very short example. Suppose all pickups have a cost, weight, icon and a prefab when you drop them. We can set that up:

 class PlayerItem {
   int cost;
   float weight;
   Texture2D icon;
   Transform prefab;
 }

 PlayerItem chicken = new PlayerItem();

Chicken now has all four variables, bundled together. chicken.cost and so on. Of course, you still have to set them. But way nicer than having to make all four chickenCost, chickenWeight ... variables yourself. There also are some extra magic words that seem redundant -- if you declare an int, it auto-makes the int; but if you declare a chicken, you also have to make(new) the chicken? Yeah, C# is just like that.

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
avatar image
0

Answer by jeebxoxo · May 02, 2014 at 02:52 PM

I'll try this and try to be as concise as possible since you understand programming and just need to get over this 'lil hump.

GameObjects are unity objects you create in your Scenes. You go to Create > GameObject to make an empty one with nothing in it. In your inspector you can see you can set Tags to it for a reference point like 'Player' and then layers if you want to customize how it interacts with other colliders and such. You could set the tag to 'Player' for instance.

Then let's say you want to reference your player GameObject somewhere else. You can do that anywhere with

 GameObject Player = GameObject.FindGameObjectWithTag("Player");

this is just like setting up a reference to anything, like:

 int count = 1;

Everything in your Scene is housed in a GameObject, and then you can add scripts to those GameObjects to do whatever you want.

To answer the second part of your question, the purpose of these 'obscure' types is that you get to define what it does because the type is the piece of code that you created. This is the beauty of generic types because you get to make whatever you want!

You name every script you add to your GameObjects, and then you can reference those too. See how I set up a reference to the player GameObject up there? Now you could access your 'Orientation' script and set your spinAmount or whatever you want.

 Orientation orient = Player.GetComponent<Orientation>();
 
 float spin = orient.spinAmount;
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Servers connection differences/uses 1 Answer

Type in bug in editor 1 Answer

Determine if game object is tyoepf One Prefab 3 Answers

Unity4, resolution-independent typography 1 Answer

Array of Transform[] not correctly typed in Javascript 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