Hello everybody,
I’m new in unity and C#. I use COBOL lol it’s différent.
I try to make a script to transform the position of differents cubes. But i think i have a probleme for initialisation.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ExampleClass : MonoBehaviour
public int i;
{
void OnMouseDown()
{
// move cube
transform.position.new Vector3(-11,0,-9+i);
i += 2
}
}
the first object when i click on move to (-11, 0, -9)
but after its always the same.
How to change to (-11,0,-7)
after (-11,0,-5)
…?
thanks for your help
Can you start by please copy and pasting your actual code? What you have shared here won’t compile. Maybe that’s the problem, do you have compile errors in Unity? Also can you please use code tags as described here to make your code more readable on the forums? Using code tags properly
If this is your actual code you have a lot of compiler issues before you can even try to run it.
“public int i;” should go inside the { } of the class.
transform.position = new …
Need semicolon after the line “i += 2”;
You may get more compiler errors as well - compiler errors will show up in the Unity console and on the bottom bar of the Unity editor, and they’ll point you to where the problems are.
Once you’ve got the compiler errors fixed you can work on the logic of the function. If I understand your goal correctly I think you’ll want to make i static, which means it’ll be shared among all copies of the script - as it is, each object has its own “i” which always starts at 0.
1 Like
before and after. the dice is a the same place than the first
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class positionClick : MonoBehaviour {
public Vector3 position;
public int i;
// Start is called before the first frame update
void OnMouseDown()
{
transform.position = new Vector3(-11,0,-9+i);
i += 2;
}
}
HELP I NEED SOMeBODY HELP YOUKNOW i NEED SOMEONE!!!
…yelling in all caps won’t get you help any faster.
You will need to share the “i” variable among the instances of your script on the different objects. The easiest way to do this for now, would be to just make i static:
public static int i = 0;
That makes the variable a part of the class definition (not of each instance of the class), and they’ll all share the same copy of “i”.
It’s probably not the best approach depending on what you’re wanting to do with these - I think eventually you’re going to want to create some sort of collection of dice and control the position with a manager object - but it should work for the short term.
1 Like