Pointers and linked lists. Maybe some UI.

I’m working on a project in Unity. I very much need a pointer to my class within my class to make a linked list of my class. C# and Unity don’t allow it. It’s easy in C++.

I have a class “Wall” each wall has a “LeftEnd” and a “RightEnd” each are class “Wall”. Each “Wall” just needs to point to the next and previous “Wall”. I’ve tried a few different ways, but they all cause errors in Unity or C#.

Just Looking for ideas on how to approach this in Unity.

I also need a context capable UI on selected objects. I can make one, but it doesn’t work on dynamically created objects. If I add my prefab in Unity the UI works perfect. When I add my object in code the UI doesn’t work. Not sure why.

Any ideas would be helpful.

Thanks

Pointers are references in C#.

So all need is to declare two fields in the Wall class for the walls attached to the left and right.

public Wall attachedLeft;
public Wall attachedRight;

Then when spawning the walls you would need to set the fields but I’m sure you know that.

As for the contextual UI not recognizing a spawned object over an object placed in the editor, you need to make sure to set the reference/pointer in the UI class to have it recognize it. I’m only making assumptions here about how your code works. C# isn’t that much different from C++ seeing as C# takes a lot of its features from C++, its the Java features that really set it apart which is that its a managed language.

Pretty much all objects by default are treated basically by reference. There’s no need to use pointers for that reason.

“First you must get the pointer, THEN… you get the power!!”

So as others point out above, references to classes are for many intents and purposes identical to pointers.

You cannot ever do pointer arithmetic on them however, even if they’re in an array. You can’t ++ them to go to the next one or subtract two of them to find out how far apart they are, or divide a block of memory by the sizeof() one of them to see how many there are. All that is verboten in C#.

HOWEVER… references like this only applies to classes, which are reference types.

If you are using a struct, such as Vector3 (struct), this is a value type.

This means what looks to the casual observer like a reference is actually a complete fresh copy of the struct.

More spiffy reading on Value Types vs Reference Types:

https://discussions.unity.com/t/826396/4

Thanks all. I’ll give that a try. It makes sense.

Pointers are fun that way. I miss them.

Funny thing is while modding Unity games I’ve used a lot of that kind of pointer arithmetic.

Oh deep far down underneath it all, it’s all just data sittin’ in RAM being processed by a CPU.

In fact, for iOS and Android and even more targets, Unity trans-codes the C# to C++ (look up IL2CPP) and compiles that.