- Home /
Pass Variable to public Variable for processing?
I thought I used to do things like this all the time but I am either having an off day or dreaming about features that I never used?
Anway, what I am trying to do is very simple. I often do this.
private string variableA = "";
variableA = variableB;
public varibaleB {
get{
return "Answer!";
}
}
// which works well right? I also often do this.
private string variableA = "";
CalculateVariableB();
public void CalculateVariableB (int numberA, int numberB){
if(numberA>numberB)
variableA = "YES";
else
variableA = "NO";
}
// Works well right? Now here is where the problem comes in, I wish to do this.
private string variableA = "";
variableA = variableB(1,5);
public string variableB(int numberA, int numberB){ //Edit: <--- This is where the problem is, while this looks good, this line does not work as I can not pass the variables into the string.
if(variableA > variableB)
return "YES";
else
return "NO";
}
I think when I used to write in Javascript it was something like
function VariableB(numberA : int, numberB : int) : string{
// do something and return a string?
}
But I am not sure I remember correctly or how to pull it off in C#.
Thanks guys.
Answer by fendorio · Apr 16, 2014 at 08:54 PM
public string x { get { return Foo(1, 2); } }
private static string Foo(int x, int y)
{
return (x > y) ? "Bigger" : "Smaller";
}
What you're looking for?
Debug.Log(x)
Would result in : "Smaller" in this case.
Yeah as the answer states below you're bug was that you weren't testing the two method parameters namely numberA & numberB
public string variableB(int numberA, int numberB)
{
if(variableA > variableB) //Should be (numberA > numberB)
return "YES";
else
return "NO";
}
lol, Thumbs up to you both. I did a horrible job trying to explain my problem but b/c you guys gave me back my answer without actually seeing my problem, I realized what I did wrong. Yes I was checking the variable vs the variable ins$$anonymous$$d of the number which was a mistake in my example by what my real problem was that I was doing this.
public string $$anonymous$$yString (string inco$$anonymous$$gString){
get{
return "some string";
}
}
// Ins$$anonymous$$d of
public string $$anonymous$$yString(string inco$$anonymous$$gString){
return "some string";
}
Thats what my problem was and I have been tippytowing around it for 2 days by doing this.
public string returnString ="";
public string $$anonymous$$yString {
get{
// do something with return string and return a string
return "some string";
}
}
which was a really annoying workaround. Anyway. Thanks for helping me see the light guys.
Glad to be of service! It's always the 'obvious' bugs which are hardest to find! Btw I've just been doing some math stuff with the console so only made the above Foo static out of habit lol. Hope the project goes well :)
Answer by MFen · Apr 16, 2014 at 08:57 PM
public string variableB(int numberA, int numberB){ //Edit: <--- This is where the problem is, while this looks good, this line does not work as I can not pass the variables into the string.
if(variableA > variableB)
return "YES";
else
return "NO";
}
change it to this
public string variableB(int numberA, int numberB){ //Edit: <--- This is where the problem is, while this looks good, this line does not work as I can not pass the variables into the string.
if(numberA> numberB)
return "YES";
else
return "NO";
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613142902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Make a laser flickering. 1 Answer
Yield Wait for Seconds (A little help here) 2 Answers
Triggers and SendMessages. C# 2 Answers