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 peterpunk · Oct 15, 2013 at 09:52 PM · c#assign-variabletypes

Setting public variables by dragging: diverse results

I am a bit confused about the relation between GameObject instance and the script (class) attached to it; using C#.

In terms of class extension, I believed that there was no relation whatsoever. I have a standard GameObject A with a class attached to it, Aclass. Now say in Aclass I have a bool variable isOn.

Now on the associated class of another GameObject, I have a public variable which is assigned by dragging the prefab A onto it. What happens?

I found out that it depends on the type you assigned to the public variable: if its of type GameObject, Unity assigns it to A, if it is of type Aclass, Unity assigns it to the class associated to A.

Is this the expected behavior? Can anyone point me to specific documentation?

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 Benproductions1 · Oct 16, 2013 at 01:06 AM 0
Share

Could you be a bit more precise. It's hard to understand what you mean by: "Unity assigns it to A"

1 Reply

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

Answer by whydoidoit · Oct 16, 2013 at 01:16 AM

Basically you've got it right there.

A reference is typed as the type of the attached script, found from the prefab with GetComponent (effectively) - so if you have a variable of type AClass (derived from Component/Behaviour/MonoBehaviour) then the reference will be to the component attached to the Game Object, but should you instantiate it, then the whole Game Object and all of its other scripts attached in the prefab will be created and the reference to the copied AClass will be returned by the Instantiate call.

If you have GameObject then that is the thing you will get back.

GameObject is almost totally useless from the Unity side of things - it's basically only function is to allow us to call SetActive and AddComponent. Of course it's doing a lot of stuff on the C++ side.

Comment
Add comment · Show 1 · 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 peterpunk · Oct 16, 2013 at 06:11 AM 0
Share

So this is what it does by design. Thanks!

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

calling a class named by a string -1 Answers

Flip over an object (smooth transition) 3 Answers

I want to assign multiple variables without having to type the class on the side every time. 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