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 Ahsenhein · Mar 02, 2017 at 01:34 AM · script.scripting beginnerscriptingbasicsscriping

How do I read this? private Rigidbody rb;

I followed my first tutorial and now I have some doubts about how C# works.

So, private Rigidbody rb; :

"Private" means It won't appear in the inspector and the only way to reach this data is by directly editing it in the script where it is found?

Rigidbody rb means that rb is a variable linked to the game object Rigidbody?

If so then the diference between "Rigidbody rb;" and "int count;" is that the first one the variable is linked to a game object/class/component and the second I simply create a variable and specify its type but with no attach/link to any object?

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 Ahsenhein · Mar 02, 2017 at 01:40 AM 0
Share

Edit: Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

Also I'd like to know what the "new" means in this code. The whole code means that the variable movement will recieve a new x,y,z, right? If so why not make it without the "new" expression? setting it to another values isn't enough?

Thank you!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Commoble · Mar 02, 2017 at 03:57 AM

"Private" means It won't appear in the inspector

Yes, though you can make private variables appear in the inspector if you need to.

and the only way to reach this data is by directly editing it in the script where it is found?

Yes.

Rigidbody rb means that rb is a variable linked to the game object Rigidbody?

No. Rigidbody rb means that your script has a variable named rb, of the type Rigidbody, and this variable may be uninitialized, or hold a null value, or hold a rigidbody that belongs to your script's gameobject, or hold a rigidbody that belongs to another gameobject (if it holds a rigidbody that doesn't belong to any gameobject you probably screwed something up). To link it to the gameobject's rigidbody, you'll either need to link the rigidbody to your script in the inspector, or make a new one and get a reference to it with this.rb = someGameObject.AddComponent<Rigidbody>() if someGameObject is a gameobject that doesn't already have a rigidbody (you can use this.gameObject to get a reference to the script's own gameobject).

then the diference between "Rigidbody rb;" and "int count;" is

Rigidbody is a reference type and int is a value type. You can read up on the difference between value types and reference types by googling them, the details aren't important here. Rigidbody is also a UnityEngine.Component, meaning you can add it as a component to a gameobject, and link it to another Component's field in the inspector if you need to. As far as your script's concerned, though, Rigidbody is just another class. Now, all those public fields that you declare in your monobehaviour and set in the inspector, what happens to those is that when the game starts up, and the scene with your gameobject loads up, and the gameobject initializes, and all instances of your classes (your scripts) initialize, Unity fills in all those public fields with the values and references that you set in the inspector in the editor, so your script can fiddle with them.

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

Also I'd like to know what the "new" means in this code.

You're creating a new instance of a Vector3 struct. You're also assigning it to the Vector3 variable you just declared.

The whole code means that the variable movement will recieve a new x,y,z, right?

Yes.

If so why not make it without the "new" expression?

Something like Vector3 movement = someOtherVectorThatAlreadyExists;? You could do that. Additionally since Vector3s are structs and structs are value types, this will just copy the values, and you'll be dealing with two different vectors now (that happen to have the same value).

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 Ahsenhein · Mar 02, 2017 at 04:23 AM 0
Share

Thank you. It's a bit more clear now.

avatar image NoseKills · Mar 02, 2017 at 05:28 PM 0
Share

It should be pointed out that while the 2 first assumptions are true in a way, access modifiers like internal, protected, private and public are not a Unity feature. They are an integral part of the C# language and many other program$$anonymous$$g languages.

The fact that variables show up or don't show in the inspector depending on these is just a design decision by Unity engineers.

Private members are only accessible whithin the body of the declaring class, so depending on what one means by "a script" , a private variable might or might not be accessible inside that "script".

avatar image
0

Answer by Ahsenhein · Mar 02, 2017 at 05:02 AM

@Commoble

Could you explain to me one more thing?

When I add a component to an object, let's say rigidbody and animator to the Player object, does it mean I always need to make this references on start/awake functions to make this component to work?

     private void Awake()
     {
         anim = GetComponent<Animator>();
         PlayerRigidbody = GetComponent<Rigidbody>();
     }


Comment
Add comment · Show 3 · 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 Intare · Mar 02, 2017 at 03:30 PM 1
Share

No, the component works always if it's attached to a GameObject and it's active.

Doing that in the Awake, or Start method, only lets you acces to this components inside the script.

So, for example, an animation controller would work always, but if you do anim = GetComponent(), you can use that script to play or pause the aniimation.

avatar image Ahsenhein Intare · Mar 02, 2017 at 04:48 PM 0
Share

So basicaly I do it If I need to create any logic in that script that needs to use any function of that component.

And, in this example, what data format would anim recieve? I know that if I declare something like Vector3 movement I know that this variable will recieve x, y and Z in this format (X,Y,Z) but for many others I get confused of what I'm setting to that variable.

Another example is this floor$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask("Floor");. Floor$$anonymous$$ask has the type int so it's an integer number but I don't see how Get$$anonymous$$ask("Floor") would return a number. Does that mean that all $$anonymous$$asks have a number like an ID?

Thank you. I've just started with unity and C# so there are millions of things to learn.

avatar image NoseKills Ahsenhein · Mar 02, 2017 at 05:53 PM 0
Share

Whenever you get confused, refer to the Unity API docs or c# API depending on which the confusing method is part of.

Basically googling "c# confusingmethodname" or "unity confusingmethodname" works like a charm usually.

Layer$$anonymous$$asks and layers are a Unity feature as well. They are classes just like any classes you could make. Layers are objects that have a name and an integer value. Get$$anonymous$$ask is a method that returns a value based on how you have set up Layers in the Insoector. How exactly the names and integers get deter$$anonymous$$ed is less relevant than knowing what you need to use them for and how, when you do.

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

100 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

Related Questions

Make enemy patrol without Nav Points? 0 Answers

Pass a reference to a button that opened the menu this script is on 0 Answers

Need to Change this script function , please 0 Answers

Swipe Detection Script not working as intended 0 Answers

Could someone translate these to c#? 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