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
1
Question by tatarinrafa · Feb 26, 2014 at 11:54 AM · objectvariable

How to assign an object to variable C#?

Im kinda new to c#. I used to work with World Editor(warcraft 3)and there you could assign an object to a variable as simple as below

set udg_MyObject = gg_unit_hfoo_0000

Where "udg_MyObject" is a variable and "gg_unit_hfoo_0000" is an actual object

I know that C# and War3 use kind of different script mechanics, so this is what i want:

I want to have a variable that i could use in many different scripts that will refer to the object i need.(the target object will also change in game so i need to use variable for it)

Comment
Add comment
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

1 Reply

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

Answer by tBureck · Feb 26, 2014 at 12:32 PM

"Kind of different" hits the nail on the head :) In C# every variable has a type, which you need to know beforehands. Assuming you have a GameObject type (which you usually have in Unity), you could create a variable like that:

 GameObject myGameObject = referenceToAGameObject;

where myGameObject would be your variable (of type GameObject) and referenceToAGameObject would be a reference to your game object, which you can get from a Transform or whatever.

I think it would go too far if I explained that in detail here, so I'd just advise you to have a look at the Learn section on the Unity website, which provides a lot of videos explaining the very basics of Unity scripting with C#.

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 tatarinrafa · Feb 26, 2014 at 06:34 PM 0
Share

Thank you! exactly what i needed. Yeah, i know that going through the manual is best way to go, but im one of those people that learn by changing some random value and testing what it will do to the game :)))

avatar image Jamora tatarinrafa · Feb 26, 2014 at 06:33 PM 0
Share

Please use the very small "add new comment" button to add comments. Answers are meant to be solutions to the asked question.

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

22 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

Related Questions

Setting a variable to an instantiated object. 1 Answer

how to make a script when u collide with an object it disappears 3 Answers

Angry Birds Style Loader (Load the Birds to be fired) 0 Answers

Passing position to function 1 Answer

Check if var is true from a random object 0 Answers


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