How can I keep a member var that refrences a public var in another class? Like in c++

Hey there, I was trained in C++ primarily for game dev but for the last two years have been using unity and C#.

I’ve pretty much had to teach myself the differences and what not and now I’m trying to do something that I can’t seem to figure out the C# “way” to do it…

Here is some quick pseudo-code of what I’m trying to accomplish… Thanks!

public class MyClass
{
         public bool bBool;
     
         private float* pData;
     
         void Start()
         {
             if (bBool)
                 pData = OtherClass.fData;
             else
                 pData = DifferntClass.fData;
     
         }
     
         public void OtherFunction(float _fNewData)
         {
              (*pData) = _fNewData;
         }
}

I think this tells you everything

Also, due to the apparent double posting of the topic, further discussion here.

Yea well, I figured scripting was the appropriate sub topic, hehe…

I guess I should have been more clear. I am aware you can’t use unsafe with unity. That is the whole reason for posting, I’m looking for the way I should be going about this.

I think from what I’ve gathered from your other reply and a lil further reading is…

I need my values in the other classes (OtherClass and DifferntClass) to share a common class type that can be referenced, although I’m still not sure on the exact code… I’m about to try something like this…

public class CommonData
{
     float fData;
     int nData; 
     ...
     ...
}
public class OtherClass
{
  CommonData myData;
}
public class DifferntClass
{
 CommonData myData;
}
public class MyClass : MonoB
{
    public bool bBool;
     
    private CommonData pData;
     
    void Start()
    {
         if (bBool)
              pData = OtherClass.MyData;
         else
              pData = DifferntClass.MyData;
     
    }
     
    public void OtherFunction(float _fNewData)
    {
         pData.fData = _fNewData;
    }
}

I don’t know much about it, but in c# you do something like:
CommonData mydata = new CommonData();

Otherwise you will get a null when you use it unless you are just using it for a reference. I think c++ and c# /java are different that way. I haven’t done any of that yet because I’m just trying to learn all the scripting functions so let us know how it works out. I’m pretty sure if it’s a mono it has to be attached to an object.

Since we are without much contextual information here, I’d like to advise to read about scriptable objects, too:

By my experience, using a common, non monobehavior class works very well, and you can make the whole class serializable to make it easier to set data with the inspector. Things are even better now with property drawers. Things can get tricky when you want a behavior like “a pointer to a pointer”, but this is very unlikely to be needed with good design.

What you showed in first pseudo-code is generally correct. You just do that but no pointers or any of that. Just use a class instead of a float so that when you do:

if (bBool)

                 pData = OtherClass.fData;

             else

                 pData = DifferntClass.fData;

it’s a reference being assign and not a value as it would be if fData was a float.

If you declare your pData as of class type then assigning it value with = operator will assign reference but if pData was declared as struct or float then = would assign value. This way you store reference in C# without worrying about pointers and all that.

Pretty new to C# myself so someone more knowledgeable might correct my but it should be right more or less.

If you always want that variable’s value to be shared across all classes, you could make it static, but not if you only want to share certain instances

It’s evident from his examples that he wants different instances of data contained in different classes.

It’s not evident to me, all of MyClass OtherClass DifferentClass are not instances of the same class.
I’m not entirely sure why you would want a reference to the public var of another class (rather than changing the value directly through a reference to the object itself) unless you wanted the value to be shared across all objects attempting to access that variable, without necessarily keeping a reference to the whole class. In that case, if you don’t want separate instances of OtherClass or DifferentClass, a static member variable would work perfectly.
The only place in these examples that instancing is happening is when using the CommonData class, but I’m suggesting a different way of handling the problem without needing CommonData

He wants to store reference of object whose filed value his changing because at that point - when assigning new value he is not necessarily aware which object is it who’s field he’s changing. Logic for choosing that object is in different place in code and that’s why he stores that choice whatever it is to act upon it some other place in code.

Also:

    public class OtherClass
    {
      CommonData myData;
    }

    public class DifferntClass
    {
     CommonData myData;
    }

Two different classes, two instances of myData. It couldn’t be any more evident - it’s right there!

Right ok, I understand that. Originally, there was no CommonData class, and there are no separate instances of OtherClass or DifferentClass in these examples. I was suggesting using a static float in OtherClass DifferentClass, not in CommonData (as a different workaround to the original problem). It being static would mean he wouldn’t need a reference to the instance of OtherClass or DifferentClass, and he wouldn’t need a different object to store the data. The object selection logic can be moved - since MyClass will know which object it needs to assign to (using the bool).

public class MyClass
{
	public bool bBool;
	
	void Start()
	{}
	 
	public void OtherFunction(float _fNewData)
	{
		if (bBool)
			OtherClass.fData = _fNewData;
		else
			DifferntClass.fData = _fNewData;
	}
}

public class OtherClass
{
	public static float fData;
}

public class DifferntClass
{
	public static float fData;
}

Again, that is all predicated on there being only 1 instance of OtherClass DifferentClass, as I said in my first post, the point could be ignored by the OP if he wanted multiple instances.

One way to do this would be to use an interface:

interface IDataSource
{
	float Data { get; set; }
}

class CommonData : IDataSource
{
	public float fData;
	public float Data
	{
		get { return fData; }
		set { fData = value; }
	}
}

class OtherData : IDataSource
{
	public float fData;
	public float Data
	{
		get { return fData; }
		set { fData = value; }
	}
}

class MyClass : MonoBehaviour
{
	public bool useOtherData = false;
	
	private CommonData  common;
	private OtherData   other;
	private IDataSource source;
	
	void Start ()
	{
		// Get common and other data from somewhere
		...
			
		if(useOtherData)
			source = other;
		else
			source = common;
	}
	
	public void OtherFunction ( float newData )
	{
		source.Data = newData;
	}
}

Yeah, I didn’t really notice that objects we chose among are of different types. So Errorsatz solution is totally the way to go. Interface or base class will do depending on needs.