Hi I have only been using Unity for a short time but have learnt a great deal. Now I am following a tutorial series specific to the type of game I want to make but the author does things in the least efficient way by repeating the same code several times that could be combined into one method and reused instead. I have managed to teach myself how to fix these things as I go but have come to a point where they have created strings then used those strings in the following code. I have attempted to create a method with this code but have not had any success with using strings as inputs for a method that are then used in the method. Any help with this would be appreciated as I am not sure where I am going wrong here.
public string productionUpgrade1CostString;
public BigDouble productionUpgrade1Cost;
public void Update()
{
ScientificNotation(productionUpgrade1Cost, productionUpgrade1CostString);
}
public void ScientificNotation(BigDouble input, string input2)
{
if (input > 1000)
{
var exponent = (BigDouble.Floor(BigDouble.Log10(BigDouble.Abs(input))));
var mantissa = (input / BigDouble.Pow(10, exponent));
input2 = mantissa.ToString("F2") + "e" + exponent;
}
else
{
input2 = input.ToString("F2");
}
}
I’m actually not sure what you’re attempting to do. If this is all under one script, there may not be a need to pass the field values amongst the methods. And even with that aside, you’re passing in input2 string value but don’t appear to be using it.
I am trying to condense the original code into one method here is a copy of two parts of the original code but it is repeated for several variables and strings basically it goes on for several pages.
Because of the way it is laid out I need to add in this whole section for each number I want to make display as scientific instead of calling a method with some variables.
Well, strings are value types, so you’re not going to get the effect you want. By setting the value to input2, you’re changing the value of input2 and not the original string. What I would do is have the method take only input1 and then return the string which you can assign to your variable instead of trying to pass it a value and a string like you’re doing.