- Home /
Trying to convert Js to C#
hi folks, im trying to convert this Js: var emptyslot : Transform; var xtemp; var ytemp;
function OnMouseUp(){
if (Vector3.Distance(transform.position,emptyslot.position)==1)
{
xtemp=transform.position.x;
ytemp=transform.position.y;
transform.position.x=emptyslot.position.x;
transform.position.y=emptyslot.position.y;
emptyslot.position.x=xtemp;
emptyslot.position.y=ytemp;
}
}
I came to this result:
using UnityEngine;
using System.Collections;
public class mov : MonoBehaviour {
public Transform vazio;
private float xtemp;
private float ytemp;
void OnMouseUp (){
if (Vector3.Distance(transform.position,vazio.position)==1f)
{
xtemp = transform.position.x;
ytemp = transform.position.y;
transform.position = vazio.position;
vazio.position.x = xtemp;
vazio.position.y = ytemp;
}
}
}
BUT this don't work. Can someone help me?
This don't work? I'm sure that's not the error that Unity gave you. Please be specific on what doesn't work and what the error message is.
For one thing, you can't set the position of 'vazio' like that. Ins$$anonymous$$d use:
vazio.position = new Vector3(xtemp, ytemp, 0);
Assets/scripts/mov.cs(17,31): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.position'. Consider storing the value in a temporary variable Assets/scripts/mov.cs(18,31): error CS1612: Cannot modify a value type return value of
UnityEngine.Transform.position'. Consider storing the value in a temporary variable
These are the errors that appeared before, Now I made the changes you suggested. It's working! VERY GRATEFUL!
Could you tell me who helped you out so I can clean up this thread please ?
Answer by Mikael-Gyth · Apr 10, 2014 at 09:36 AM
@ivmarel is correct, you are probably getting an error for trying to set the readonly values of position.x and position.y
using UnityEngine;
using System.Collections;
public class mov : MonoBehaviour {
public Transform vazio;
private float xtemp;
private float ytemp;
void OnMouseUp (){
if (Vector3.Distance(transform.position,vazio.position)==1f)
{
xtemp = transform.position.x;
ytemp = transform.position.y;
transform.position = vazio.position;
vazio.position = new Vector3(xtemp, ytemp, 0);
}
}
}
Atleast that is the only thing wrong with the script, but ofcoure there can be other issues that cause it, but that is hard to tell without the actual error message.