C# object destroys reference to string ( I think)

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Nov 28, 2011 at 23:04

I want to see if anyone can help me here before I try Unity, but my problem has to do with C# in Unity.
I have a string which acts as a caption and disappears after a certain amount of time.
I get the string from a game object script which has a string in it so it looks something like
setCaption(object.component.aString);
Everything works fine unless I destroy the object right after I get that caption. Then it disappears. I assume this is because anything referencing the object destroyed gets destroyed also or loses reference. I looked around on c# and they have a copy method which supposedly creates a new instance, but same thing happens. So, does anyone know how I could keep from losing that string?

EDIT: Nevermind. There’s something weird going on because I can use setCaption(“a string”); and the same thing happens when I call the object to be destroyed.

4 Replies

Please log in or register to post a reply.

6837d514b487de395be51432d9cdd078
0
TheNut 179 Nov 29, 2011 at 02:23

Strings in C# are immutable, so you don’t have to worry about dereferencing. Even if you were passing around references, the GC isn’t going to surprise you one day and kill off something because it does keep track of references (unless you explicitly use weak references). This is likely an internal problem with the display mechanism. Have you ever looked into the MVC pattern and data binding? It’s a very elegant way to propagate data into user controls without having to write code explicitly to do that. For example, your label control could be data bound to a model variable “Caption”. When you modify the caption property, a binding update triggers the user control to update its display. Via this decoupling, it’s easy to debug UI problems because you just have to look at the model to see if the data is set or not.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Nov 29, 2011 at 02:33

This is likely an internal problem with the display mechanism. Have you ever looked into the MVC pattern and data binding?

I haven’t looked into that, no. Not sure how it would align with Unity’s GUI system. I think I’m referencing the object in another place in the program and that’s what’s causing the problem when I destroy it. I should have known because things were going too easy. :)

6837d514b487de395be51432d9cdd078
0
TheNut 179 Nov 29, 2011 at 03:13

Yes, heaven forbid we make life easy :lol: I haven’t used Unity so I don’t know how it manages UI. I read you called a method “setCaption”, which made me think back in the day with Win32 and MFC stuff (/me shuddering). MSDNI think has the only article out there that describes data binding in sufficient detail, but the concept applies to any framework, language, or platform. I’ve written ports for several other languages and it’s a lifesaver. If Unity doesn’t have something equivalent to that, it shouldn’t be impossible to write a wrapper for it.
@fireside

I think I’m referencing the object in another place in the program and that’s what’s causing the problem when I destroy it

That seems weird to me because strings are immutable. The label control will have its own instance of the string, newly created once you call the setCaption method. I’d be more inclined to investigate if the label control is dereferenced or disappearing somehow (logic issue?).

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Nov 29, 2011 at 03:44

That seems weird to me because strings are immutable. The label control will have its own instance of the string, newly created once you call the setCaption method. I’d be more inclined to investigate if the label control is dereferenced or disappearing somehow (logic issue?).

It was in another part of the program where a feature tested for a null after a ray cast and set the caption to nothing if it found one. When I deleted the object, it found a null. I kind of forgot about it. One was doing a mouse-over, one was doing a mouse-click when it was over something. It all worked fine until I deleted an object. The setCaption is my own function and I’m sure worse than anything Microsoft could imagine. Unity code is pretty weird though, to my way of thinking. The GUI is the strangest I’ve seen but it works all right. Not the most object oriented code around. You don’t need much GUI for a game, some buttons and labels.