Converting from Java
I have been trying to convert this to C# but it doesn't like it. The Java code works and is here.
public var slot: Transform;
var xtemp;
var ytemp;
function OnMouseUp(){
if (Vector3.Distance (transform.position, slot.position) == 1.0) {
xtemp = transform.position.x;
ytemp = transform.position.y;
transform.position.x = slot.position.x;
transform.position.y = slot.position.y;
slot.position.x = xtemp;
slot.position.y = ytemp;
}
}
This is what I have for the C# so far:`using UnityEngine; using System.Collections;
public class SlidingBlocks : MonoBehaviour {
public Transform emptySlot;
private Transform xtemp;
private Transform ytemp;
void OnMouseUp () {
if (Vector3.Distance (transform.position, emptySlot.position) == 1.0f) {
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;
}
}
} ` Any help would be appreciated.
Answer by Hatsuko · Oct 26, 2017 at 12:03 AM
You can look at the Console window and see what it tells you. Double click on them, and it will bring you to the line that may be causing error.
You should get two types of errors now:
Cannot implicitly convert type 'float' to 'UnityEngine.Transform'
This will bring you to: xtemp = transform.position.x;
You are assigning transform.position.x
to xtemp
. transform.position.x
is a float, and xtemp
is a Transform because you declared it as so. So basically it's telling you that this can't be done, because it cannot convert a float to a Transform. Change xtemp
and ytemp
's type to float and this error will be gone.
Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable
See this post
Answer by Cuttlas-U · Oct 26, 2017 at 06:17 AM
hi; the only problem in converting java to c# is the part that u want to change a value like x or y or z from a vector 3 ; in java u can do it directy like this :
transform.position.x = emptySlot.position.x;
but in c# u have to move it to a temp variable and then assign it again like this :
Vector3 Temp = transform.position;
Temp .x = emptySlot.position.x;
transform.position = Temp;
Answer by twetzel2k · Oct 27, 2017 at 05:49 PM
Ok, I am getting some where, but this code still doesn't do what it should. No errors, but the two objects do not switch position.
public class SlidingBlocks : MonoBehaviour {
public Transform emptySlot;
void OnMouseUp () {
Vector3 Temp = transform.position;
Vector3 Temp2 = transform.position;
if (Vector3.Distance (transform.position, emptySlot.position) == 1.0f) {
Temp.x = emptySlot.position.x;
Temp.y = emptySlot.position.y;
transform.position = Temp;
emptySlot.position = Temp2;
}
}
}
Put Debug.Log("!!!"); before and after if (Vector3.Distance (transform.position, emptySlot.position) == 1.0f) {
to see if it reaches into if-statement or not
By "switch position" do you mean "switch position x, y, z" values? If so you miss one line Temp.z = emptySlot.position.z;
. Also try to create a new scene to test (see below gif) so you make sure it isn't effected by other scripts or stuff. If it works in the new scene but doesn't work in the original scene, then something else in the original scene is causing trouble. Also this is not about converting from JavaScript to C# anymore, so it's better if you post a new question.
Your answer
Follow this Question
Related Questions
How to load level after typing somthing? 1 Answer
SHA512 JS -> C# 0 Answers
Help converting a few lines of JS to C#. 1 Answer
Js to C# conversion without error but can't work 2 Answers
JS to CS conversion problem with multiple class files 1 Answer