- Home /
storing the value in a temporary variable problem
Hello people, i want to make a pinball game fully in C#, i am using the gameion pinball template but rewrite it in C#. i run into the following problem when firing the pinball:
if (CanUse == true) { ControlBallLaunchByKeyboard(); } else { // Spring resets back to normal position if (ballLaunch.localScale.y < 12) {
ballLaunch.localScale.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;
ballLaunch.position.y = (ballLaunch.position.y - 63.00549f) * 0.5f;
}
}
}
private void ControlBallLaunchByKeyboard (){
if (Input.GetKey(KeyCode.DownArrow))
{
// When the player holds down the Down Arrow button, pull down the ball launch
if (ballLaunchForce < 2000)
{
float dt = Time.deltaTime * 8.0f;
ballLaunchForce += dt * 180;
ballLaunch.localScale.y = Mathf.Lerp(12.8f, 1.8f, ballLaunchForce / 2000);
ballLaunch.position.y = Mathf.Lerp(-63.1f, -68.6f, ballLaunchForce / 2000);
}
}
else
{
// Spring resets back to normal position
if (ballLaunch.localScale.y < 12)
{
ballLaunch.localScale.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;
ballLaunch.position.y = (ballLaunch.position.y - 63.00549f) * 0.5f;
}
}
i get allot of the following errors: - Assets/Scripts/Trig.cs(43,36): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.localScale'. Consider storing the value in a temporary variable - Assets/Scripts/Trig.cs(44,36): error CS1612: Cannot modify a value type return value of
UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Could anyone point out what i am doin wrong or help me ??
thanks in advance
Answer by moichezmoi · Mar 29, 2012 at 08:48 AM
Hi,
transform.localScale and transform.position are properties, in C#. So, you can get those vector3, you can set those vector3, but you can't dirrectly modify members of those vector3.
A solution is to use a temporary variable, and replace lines like :
ballLaunch.localScale.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;
by
Vector3 temp = ballLaunch.localScale;
temp.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;
ballLaunch.localScale = temp;
Thanks $$anonymous$$oichez$$anonymous$$oi,
I inserted your solution, and it worked. The errors are gone now.
Some new ones arrived when i start the game. Do you know what it means?
what i am ingame trying to do is when arrowkey down is pressed it should build up force and show the spring pulling back and then when release the spring resets and gives the ball force.
NullReferenceException UnityEngine.Transform.get_localScale () (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:78) Trig.Update () (at Assets/Scripts/Trig.cs:40)
when i click on the error, it says the error lies somewhere here: if (ballLaunch.localScale.y < 12)
I would guess your "ballLaunch" variable is not set to any object. Where do you assign a Transform to it?
Your answer
Follow this Question
Related Questions
converting javascript to c# 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Converting this JS code to C# 1 Answer
variable js to c# 1 Answer