- Home /
Can you store a reference to a primitive value?
I have a class that stores a large array (it holds one scalar double value for each vertex of the mesh it's attached to). I want to give the user the option to display the scalar value at a specific vertex on some UI text. But accessing the array of my original script over and over again in the Update function seems wasteful.
I would actually like to just keep a permanent reference to that specific double value in my original array, so I can just do an array access once, and my reference will forever be able to tell me the value there.
public class Example : MonoBehaviour
{
public double[] scalars = new double[mesh.vertices.Length];
void Awake()
{
...fill scalars...
}
void Update()
{
...scalars changes...
}
}
public class ExampleInfoDisplay : MonoBehaviour
{
>>>>>>> public ref double numReference; <<<<<<<
public int indexToFollow;
public Text uiText;
void Start()
{
numReference = gameObject.GetComponent<Example>().scalars[indexToFollow];
}
void LateUpdate( )
{
uiText.text = numReference.ToString();
}
}
This might not seem like that much work, but because of how the rest of my system works, my real script actually has to do a lot of nested array accesses to get the correct value, since I'm using Dictionaries paired with other arrays and so on. The real access in my LateUpdate looks more like:
public class ExampleInfoDisplay : MonoBehaviour
{
...
Example example;
void LateUpdate()
{
uiText.text = example.scalars[example.vertDictionary[example.uniqueVerts[indexToFollow]][0]].ToString();
}
}
I know that I could just make a wrapper class called Double which stores a double value, and then make scalars an array of Double instead of an array of double. I would like to just be able to store a single reference when I need it, since an array of 64-bit doubles is already quite large, and I would rather not have to store an array of 64-bit references which each have their own 64-bit values.
TLDR: I know that you can pass values by reference to methods using the ref, in, or out keywords, and that knows how to create a one-time reference to that primitive value. But what I need to know is, can you store that reference somehow and reuse it?
Can you explain what it is you are trying to achieve because it all seems a little confused.
Answer by sacredgeometry · Aug 09, 2019 at 09:08 PM
Numerical types are structs. They get passed by value (Sort of Copied). Just store a copy of its value or a copy of its index and then you can look it up when ever you need it.
Note: You can use pointers to structures in c# in unsafe code blocks. Please dont.
So, I know that by storing the index I essentially have a pointer to that value, but I have run into some other problems where having a pointer to that exact value would be useful. A pointer so that I don't need to go find the value every time, I just have my own personal pointer to that exact value.
You dont need a pointer you have the value. If you want to store the value in a variable you have a pointer to a memory address with that value stored in it.
You cant store a reference to that part of the memory space easily in c# and neither do you need to.
If you are trying to look up the same part of an array where the value is likely to change, i.e. the 10th value just use the index. The lookup is no more expensive than looking referencing a variable.
If you are trying to always have the value cached then just save a copy into a field/ property.
If you want to have it automatically do that for you then use a property with a getter that just does it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612210404im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing class data with a reference? 2 Answers
Problem with a Unity Scripting example from the scripting manual! 0 Answers
[c#] Save an instance for future re-instantiation 0 Answers
c# How does an == decide if two objects are the same? 1 Answer
Retain references to child gameobjects while duplicating objects 0 Answers