convert javascript to c#

hi all i have a script here im trynig to change to c# but failing keep getting errors
here you will see why im confused i get error saying

Assets/C#Scripts/Player.cs(23,27): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable

  if (transform.position.y <-20)
    		{
    		transform.position.y = 3.5f;
    		transform.position.x = 0.2f;	
    		transform.position.z =-0.1f;	


    		}

Can I suggest accepting an answer when it correctly answers your question? You continue to post questions without giving previous responders the thanks they deserve.

–

1 Answer

1

For this, you need to follow the advice in the error message : Consider storing the value in a temporary variable

Let’s look at that.

if (transform.position.y < -20)
{
    // build a temporary Vector3
    Vector3 tempPosition = new Vector3( 3.5f, 0.2f, -0.1f);

    // assign the Vector3 to the transform.position
    transform.position = tempPosition;
}     

This is untested and C# is not my native language =]


Here’s some links I found useful in converting between C# and JS :

unifycommunity.com?

You can also do it this way: transform.position = new Vector3(1f, 1f, 1f);

–

Sure =] I was just trying to explain the error message and the theory behind what was going wrong. I felt that this approach was best for the skill level of the OP, in the hope that stepping through the process, some understanding might occur (that lightbulb 'Eureka' moment!). Optimizing can always be achieved after the logic is understood. Thanks for your input, and I understand your frustration at the lack of resolution in other answers. It happens my friend, just think of the future readers who will be happy they found this information =]

–