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 Azial · Oct 23, 2013 at 05:22 PM · c#objectreferencetransfer

C# transfer reference of object

Hi,

how can I "transfer" or "relink" an reference of an object from one to another?

Background: I have a script Bar on an prefab gameobject with 2 array fields: Item and CdControl (both classes from me too). The Item hold information such icon, cooldown, etc and the CdControl is a script which controls its prefab-gameobject for showing cooldowns and icons from the Items in the Bar. I instantiate the CdObjects where the CdControl script is attached as CdControl and store them in the first array. This happens in the Awake() Monobehaviour when I instantiate the Bars (because there are no constructors in Unity).

At this point I can walk in a dummy environment with some gameobjects with the Item-scrip attached. I can pick up Items, then they are placed in the Bars and I can use them (cooldowns and Icons updated).

Now I want to make the a drag and drop routine for the Items, so that Items can be moved to other slots in the Bar. To achieve this, I want to relink the reference from the dragged Item to the drop-slot. The code below don't work :(

 //get coordinates normalized on iconsize
     Vector2 MouseGridCoords() {
         return Input.mousePosition/gridTileSize; //for testing this is ok, later we need an offset
     }
     
     //drag and drop items between bars
     void DragAndDropItems() {
         //drag
         if (Input.GetMouseButtonDown(0)) {
             dragItem = null;
             mouseGridBarDrag = (int)(MouseGridCoords().y);
             mouseGridSlotDrag = (int)(MouseGridCoords().x);
             if ((mouseGridBarDrag < barCnt) && (mouseGridSlotDrag < slotCnt)) { //mouseposition in grid-range
                 dragItem = bars[mouseGridBarDrag].items[mouseGridSlotDrag];
                 if (dragItem == null) return; //only pick up item, not empty slot
             }
         }
         //drop
         if (Input.GetMouseButtonUp(0)) {
             int mouseGridBarDrop = (int)(MouseGridCoords().y);
             int mouseGridSlotDrop = (int)(MouseGridCoords().x);
             if ((mouseGridBarDrop < barCnt) && (mouseGridSlotDrop < slotCnt)) { //mouseposition in grid-range
                 dropItem = bars[mouseGridBarDrop].items[mouseGridSlotDrop];
                 if ((dropItem == null) && dragItem) { //if empty slot and valid dragItem
                     //dropItem = dragItem;
                     //dragItem = null;
                     bars[mouseGridBarDrop].items[mouseGridSlotDrop] = dragItem;
                     bars[mouseGridBarDrag].items[mouseGridSlotDrag] = null;
                     RefreshSlots();
                 }
             }
         }
     }
     
     void RefreshSlots() {
         for (int i = 0; i < barCnt; i++) {
             Bar bar = bars[i];
             for (int j = 0; j < slotCnt; j++) {
                 Item item = bar.items[j];
                 if (item) {
                     bar.cdObjects[j].SetIcon(item.icon);
                     bar.cdObjects[j].SetValueD((item.count).ToString());
                     bar.cdObjects[j].SetTimeValue(item.cdLeft/item.cd);
                 }
             }
         }
     }
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 Rustam-Ganeyev · Oct 23, 2013 at 05:33 PM 1
Share

there's no reference type for int & float in c#. But you can use nullable types.

2 Replies

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

Answer by Azial · Oct 23, 2013 at 06:18 PM

Well, I found a bug that let me think the code don't work (CdObjects are not updated if the Item is null). Actually it works!

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
1

Answer by Hoeloe · Oct 23, 2013 at 05:27 PM

Primitive types such as int and float are not reference types, but value types. You can't assign null to one, because that doesn't semantically make sense. What you did would work for most objects, but since primitive types are uncomplicated, you will almost never need to do that, except in very, VERY obscure circumstances. In most cases it's easily enough to just copy the value over.

For reference types, what you wrote at the start would work (though it would throw an error for primitive types).

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 Azial · Oct 23, 2013 at 05:47 PM 0
Share

Did you even read the complete question :)? I am aware that this can't be done with value types (as I wrote), it was just an example. I want to "re-reference" an object.

But yeah, the example might be confusing, I delete it.

avatar image Hoeloe · Oct 23, 2013 at 05:53 PM 0
Share

Did you even read the complete answer? The example you posted would do exactly that, just not for value types.

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

An object reference is required to access non-static member 1 Answer

Multiple Cars not working 1 Answer

C# Null Object Reference - Driving me mad! 1 Answer

Distribute terrain in zones 3 Answers

Do you have to set a object reference for every script? 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