- Home /
got problem with x's position!
Trying to get the position of an Instance from one code. Here goes my first code:-
var redCube: GameObject;
var blueCube: GameObject;
var currentCube: GameObject;
var thegrid = new int[7,5];
var cubeSwitch: boolean = false;
var flag:boolean = false;
currentCube=redCube;
function Start()
{
Instantiation();
}
function Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if(currentCube==redCube)
{
currentCube=blueCube;
}
else if(currentCube==blueCube)
{
currentCube=redCube;
}
Instantiation();
}
Debug.Log(currentCube.GetComponent(forCube).transform.position.x);
}
function Instantiation()
{
Instantiate(currentCube, Vector3(0,0,0),Quaternion.identity);
}
There is one more code, which is attached to cubes. And that is:-
var flag: boolean = false;
var control:boolean=true;
function Update ()
{
if(control)
{
if (Input.GetKeyUp(KeyCode.LeftArrow)&&transform.position.x>-3)
{
// Debug.Log("left arrow");
transform.position.x--;
}
else if
(Input.GetKeyUp(KeyCode.RightArrow)&& transform.position.x<3)
{
transform.position.x++;
}
if(Input.GetKeyDown(KeyCode.Space))
{
flag=true;
control=false;
}
}
if(flag)
{
if(transform.position.y>-5)
{
transform.position.y--;
}
}
}
According to my first code the cube get instantiated at 0,0,0 positions but in the log screen it shows -10 for once then 0 then -10 then 0, per instantiation. Can anyone please tell me where have I gone wrong???
But... what did you seriously expect? What I can see here is you swapping between the red and the blue cubes, and reporting the x-axis position of one every time you swap. The instantiated object doesn't get taken into account at all. If the red and blue cubes are prefabs, then the position won't mean anything at all.
Here I am trying to make a grid(7x5) filled with red cubes and blue cubes. In-order to find which column the cube should fall I need to know the x-position. can u please suggest something.
Answer by ArunChnadran · Mar 20, 2012 at 02:48 PM
The instantiated value dosen't have the instance. This was my problem. Here are the things that I changed/added:-
var extraCube:GameObject; // a variable to store the Instantiated value.
then, in function Instantiation:-
function Instantiation()
{
extraCube=Instantiate(currentCube, Vector3(0,0,0),Quaternion.identity);
}
now in the main program:-
Debug.Log(extraCube.GetComponent(forCube).transform.position.x);
Answer by DaveA · Mar 12, 2012 at 11:26 PM
Could you describe your project more? Is it like Tetris or are you pre-populating a grid?
You get the x coord of anything by getting its transform. Ex:
currentCube.transform.position.x
Your code will create a new red or blue cube at 0,0,0 every frame. My guess is one of your red or blue prefabs has a -10 and the other has 0.
This code:
currentCube.GetComponent(forCube).transform.position.x
would be better as
currentCube.transform.position.x
" In-order to find which column the cube should fall I need to know the x-position." x-position of what exactly? Your cubes are either at zero or -10 when created. It really seems like there needs to be more interaction here, like with the mouse or keys or time or something. Creating 60+ cubes per second without stopping, that's a memory leak.
the game is like four balls. put four adjacent cubes of same colour in vertical or horizontal or diagonal. Then one thing is that it won't create 60+ cube at every frame only one when the space bar is pressed(thats what I saw in the hierarchy). I need to know the x-position of the cube. $$anonymous$$ore about the game, It has 7 columns and 5 rows, red cube get instantiated at 0th position and can move 3 columns left and 3 columns right, when the user press the space bar the cube fall down to the bottom, then another cube gets instantiated (with same features, but blue, player 2).Since the cubes fall on top the other, when the cubes fall it need to check in which row it should stop(y-position). then I'll save values in the grid and compare them and check whether any one had won. Hope you are clear!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613065031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create a straight gradient equation with a grid of 3d objects 1 Answer
How do I freeze a gameObject when MouseDrag runs over a certain Vector3? 1 Answer
Create an animation with variables? 1 Answer
not instantiating at correct transform 2 Answers
Object not moving,Object not moving in any direction 1 Answer