- Home /
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);
}
}
}
}
there's no reference type for int & float in c#. But you can use nullable types.
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!
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).
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.
Did you even read the complete answer? The example you posted would do exactly that, just not for value types.