- Home /
Use one variable to reference another variable
What I mean by this is that a boolean is being sent to the public void on another script and I want to use that boolean to reference a variable in that script with the same name. In the example below the string being sent to void collected is "structure" which is the same name as the boolean in the script. The example below doesn't work because monument = true isn't valid it's just to show what I'm trying to aim for.
public bool structure = false;
public void collected(bool monument)
{
monument = true;
}
Answer by BastianUrbach · Apr 11, 2018 at 06:32 AM
Is the 'ref' or 'out' keyword what you are looking for? An out-parameter is a variable passed to a function that will be assigned a value by the function. A ref-parameter is similar but it can serve both as input and output.
public void Method1() { bool someBool = false; Method2(out someBool); // Sets someBool to true } public void Method2(out bool b) { b = true; }
Edit:
For completeness and to keep things tidy, here are the other solutions I mentioned in the comments:
Reflection:
Provides a layer of abstraction for basically all C# language features, allowing things like querying for members by their name or listing members of a certain type.
// Set the value of the field named 'name' on the object 'obj' to 'value' void SetSomeField(object obj, string name, object value) { obj.GetType().GetField(name).SetValue(obj, value); }
Delegates and Lambda Expressions:
Provide a way to pass code and with it the required variables and functions between objects, functions and scopes:
using System; class SomeClass { public Action<bool> setter; void SetValue(bool v) { setter(v); } }
class SomeOtherClass { public SomeClass someObject; public bool someBool; void PassSetterToObject() { // Give 'someObject' a function that can be used to set the field 'someBool' someObject.setter = v => someBool = v; } }
So to better make sense of what I am doing with $$anonymous$$e; a string is sent to the public void and that that strings string (pretty much what it contains) must reference the name of the boolean and change its value. So the string might say 'rock' and the booleans name is rock and I want to use that string thats been sent through to reference the boolean called rock
Oh, ok that's slightly different. Accessing a variable by its name (as a string) is part of reflection (possible in C# via the System.Reflection namespace). Before you do that however, you should make sure that you really need it as using Reflection is slower, less safe and not as easy as "normal" C#.
Here is how it works:
The entry point to reflection is a type object (an object that represents the type of a value), you can get it using value.GetType(). Then you would need to find a FieldInfo object in that type object that corresponds to the field you want to access. Use type.GetField() for that. Finally you need to set a value for that field using fieldInfo.SetValue(obj, value).
So you probably wouldn't recommend doing that? Or would it be best just to do it the really long way with a whole bunch of if statements
You should look into Dictionary or Tuple. You'd have to create your own Tuple since .Net version in Unity does not include Tuple class.
Your answer
Follow this Question
Related Questions
Shooting while reload anim os playing 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Change a Variable with another script not working (C#) 4 Answers
Calling a variable in C#? 1 Answer