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 Crouching-Tuna · Nov 22, 2014 at 08:46 PM · referencemonobehaviourassign

Assign a reference to a Monobehaviour

Hi, I'm not sure if this is possible, but basically I wanna do:

 void SpawnItem(Item item){

     GameObject newItem = Instantiate (itemIcon, transform.position, Quaternion.identity) as GameObject;
     newItem.GetComponent<Item> () = item;

 }

And lets just say I call that function after I figure out which item from my inventory to spawn. I have a List(Item), and Item is derived from Mono. I want to know if it's possible to: 1. Spawn a Gameobject with the component directly referring to the same instance from my List 2. When I pick up the Gameobject, I "take" the component, put it in my list, destroy the Gameobject

This is actually related to Unity UI drag/drop icon. When I drag and drop an icon, I want each icon to also bring the correct reference of the instance. Whenever I do that, the newly created icon in my inventory(that has a ), always have the default instance.

I tried putting a

 ItemObject itemS = newItem.GetComponent<Item> ();
         itemS = item;

But of course that just refers itemS to the (Item) component in the newly instantiated object, then refers it to item, thus not really changing what newItem.GetComponent refers to at all.

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
0
Best Answer

Answer by Kiwasi · Nov 22, 2014 at 10:46 PM

This can't be done as written. Components don't exist separate from GameObjects. You can change the properties of the component to match the old one.

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 Crouching-Tuna · Nov 23, 2014 at 05:02 AM 0
Share

So there is no way to make the component instance actually referring to 1 of the instance of the class type in my List somewhere?

I probably haven't properly understood the $$anonymous$$onoBehaviour.

If I have List of Item, and I add that list from an existing GameObject(add the component), then I delete the GameObject, the reference for the instance of the component would still exist in the List, right? Or would the Item instance "die" without it living as a component to a GameObject?

I hope my question makes sense. And I hope that question is actually relevant to my problem. And, can you suggest any further reading about this?

avatar image Crouching-Tuna · Nov 23, 2014 at 05:04 AM 1
Share

"You can change the properties of the component to match the old one."

But all in all I can't have it "point" to the old one, right? Unless maybe 1 of the member is a reference type and it's pointing to something else, that reference can be passed to the new one and the reference will be identical, right?

avatar image Kiwasi · Nov 23, 2014 at 10:33 AM 1
Share

You hit it on the head there. Give the $$anonymous$$onobehaviour a reference to a non monobehaviour class. Then you can reference the identical instance as often as you like.

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

27 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

Related Questions

Error message "variable not assigned" ? 1 Answer

Is there a way to reference a script with a string? 1 Answer

How to reference an EditorWindow script in a MonoBehaviour script OR how to pass variables from a EditorWindow to a MonoBehaviour script? 1 Answer

Public variables null after assigned in the inspector. 3 Answers

How assign gameobjects to a string and reference them again? 3 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