Is it possible to use a pointer for a active GameObject. I'm not 100% how to use pointers but I think they are more efficient.
How do I assign a pointer?
Is this correct?
GameObject* myGameObjectPointer
Can the * be used in a parameter?
Is it possible to use a pointer for a active GameObject. I'm not 100% how to use pointers but I think they are more efficient.
How do I assign a pointer?
Is this correct?
GameObject* myGameObjectPointer
Can the * be used in a parameter?
Unity doesn't support unsafe code which is the only way pointers can be achieved in C# (by using the unsafe keyword). Unity does not support this keyword so you can't use pointers.
As an addendum to GesterX's answer:
GameObject is a reference type variable, which in the .Net subsystem is already implemented as a pointer to a memory adress. The only effective difference between a C# reference type variable and a C++ pointer to an instance of a class, is that you can't do pointer arithmetic with it. (Pointer arithmetic is lowlevel stuff like adding 1 to the pointer to cause it to point to the next memory adress).
Reference types in C# exhibit "pass by reference" behavior when you pass them back and forth between method calls, that is, in the following:
void Method(GameObject someObject)
{
}
void AnotherMethod()
{
GameObject NewObject = new GameObject();
Method(NewObject);
}
What is passed is actually a copy of the reference to `NewObject`. That means if you set it to null in `Method()`, the original object doesn't get set to null, because you only set the passed copy of the reference to null. If you want the actual reference to get passed, and not just a copy of it, you can add the ref-keyword like so:
void Method(ref GameObject someObject)
{
}
void AnotherMethod()
{
GameObject NewObject = new GameObject();
Method(ref NewObject);
}
If you set `someObject = null;` in that version of the two methods, NewObject ceases to exist as well.
The ref keyword works on value types as well; they are pass-by-value, but the ref-keyword causes the original variable to be passed rather than a copy of its value.