- Home /
Help with moving instantiated object's transform
Hello guys, this is an edited question. I reread what I originally posted and decided that I need to explain what I need help with better. Okay let me piece this together carefully.
//The black squares are instantiated at start at random from the Top row, to the row just above the Yellow.
//I need the blocks to move down EXACTLY -1.25f on the Y axis. In other words the block will move down 1 space. (They all need to move together).
//Only the black block closest to the yellow will execute this command, the others should not do this until they have reached the y axis of -3.75f.
//The gameobject's name is "BlackTileClone" with a tag of "BlackTile"
//I send a raycast to mouse position with a if statement of "touchcount > 0"
//I've tested various methods like FindGameObjectWithTag and GameObject.name and my results weren't so good. I tried using a foreach loop and it appears that my -1.25 was multiplied by the number of instantiated blocks. (numBlackBlocks * -1.25).
//I know the answer is right in front of my. I feel as though I've stressed myself out to the point that everything is complicated. >.<
Here is the picture of my game.
I have added some code that I feel is very close and precise. I put a lot of effort into commenting out every decision I made.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
private bool canexecute;
private GameObject[] grabBlackClonesArray;
//We created our bool to determine if the cube is at -3.75f
//We also created the GameObject Array
void Start()
{
//We set the array to have slots 0,1,2,3 (total of 4)
grabBlackClonesArray = new GameObject[3];
//We debug to see if the position 0 is not null
Debug.Log (grabBlackClonesArray[0]);
}
void Update() {
//We finally set grabBlackClonesArray and fill it up with all objects
//with the tag "BlackTile"
grabBlackClonesArray = GameObject.FindGameObjectsWithTag("BlackTile");
//We check to see if it is true
Debug.Log (grabBlackClonesArray[0]);
//To make sure that the movement is only done once we put it in a loop
//remember we are in the Update method..it's called every frame
for(int i = 0; i < 1; i++) {
//With this code the Black Block in slot [1] SHOULD only move EXACTLY
//-1.25f from it's CURRENT location. However this isn't true.
//I get these numbers, far from being exact.
//X:1.657822 Y:-0.6175802 Z:-13.79006
//Also somewhere around here is were we would include our bool
//to check if the block in [1] is in the -3.75 Y position, and if it is it can be executed then moved up top
//in a random vector (Reusing resources)
grabBlackClonesArray [1].transform.position = new Vector3 (transform.position.x, transform.position.y -1.25f , transform.position.z);
}
}
}
It appears as though it's because my instantiated objects are placed using Random.Range. The blacktile that I change the vector value of ALWAYS stays in the same place with each game restart. It is no longer random like it should be.
You are my next best friend, I tested your code (grabBlackClonesArray [1].transform.position -= new Vector3 (0.0f, -1.25f, 0.0f);) and it worked perfectly. However, the - made it go up so I went to a positive. Now I just need all black blocks to move like this.
Ah, yes I had a double negative, glad it works. I've updated and converted it to an answer.
By reading the code you gave me, I am seeing that when you put -= you are taking the current value and subtracting any value. So that is why X and Z are 0f. If I were to change them they would move as well. I feel really dumb...How could I forget the basics >.<.
Answer by T27M · May 25, 2014 at 10:53 PM
At line 16 in the code above
grabBlackClonesArray = new GameObject[3];
This will declare an array of 3 elements (0,1,2). It is 0 index, but you still declare the size as the number you want i.e if you want 4 you do
grabBlackClonesArray = new GameObject[4];
That will give you (0,1,2,3).
What happens if you replace this line
grabBlackClonesArray [1].transform.position = new Vector3 (transform.position.x, transform.position.y -1.25f , transform.position.z);
with
grabBlackClonesArray [1].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
Your answer
Follow this Question
Related Questions
transform position and rotation of instantiated object 1 Answer
Getting transform position of last parent 2 Answers
Instantiate Bullet Hole position and rotation, Vector3 issues 1 Answer
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers
Copy position from one object to another on one axis 0 Answers