- Home /
Question by
united4life · Apr 14, 2013 at 11:49 AM ·
gameobjectmove
Moving array of gameobjects.
using UnityEngine;
using System.Collections;
public class Script: MonoBehaviour
{
public GameObject [] GO = new GameObject [4];
private bool isClicked;
private int moveGO;
void Update ()
{
for(int i=0 ; i<4; i++)
{
if(GO[i] != null)
{
if(isClicked)
{
GO[i].transform.localPosition = new Vector3(GO[i].transform.localPosition.x + avatarPosition, GO[i].transform.localPosition.y, GO[i].transform.localPosition.z);
isClicked = false;
}
if(GO[i].transform.localPosition.x > 8)
GO[i].transform.localPosition = new Vector3(-4, GO[i].transform.localPosition.y, GO[i].transform.localPosition.z);
if(GO[i].transform.localPosition.x < -4)
GO[i].transform.localPosition = new Vector3(8, GO[i].transform.localPosition.y, GO[i].transform.localPosition.z);
}
}
}
void OnGUI()
{
for(int i=0; i<4; i++)
{
if(GO[i].active == true)
{
//Next gameObject
if(GUI.Button(new Rect(600, 200, 50, 50), ">"))
{
isClicked = true;
moveGO= moveGO+ 4;
}
//Previous gameObject
if(GUI.Button(new Rect(100, 200, 50, 50), "<"))
{
isClicked = true;
moveGO= moveGO- 4;
}
} }
There are 4 gameobjects and i have stored them in an array. On button click i want these objects to move by unit 4. Whenever i am pressing the buttons only 1 game object is moving because immediately i am making isclicked as false. I know this is easy but somehow i am not able to solve. Please help
Comment
Best Answer
Answer by GuyTidhar · Apr 14, 2013 at 11:54 AM
within Update() change to the following:
bool resetClick = false; // Add this
for(int i=0 ; i<4; i++)
{
if(GO[i] != null)
{
if(isClicked)
{
GO[i].transform.localPosition = new Vector3(GO[i].transform.localPosition.x + avatarPosition, GO[i].transform.localPosition.y, GO[i].transform.localPosition.z);
resetClick = true; // Change this
}
if(GO[i].transform.localPosition.x > 8)
GO[i].transform.localPosition = new Vector3(-4, GO[i].transform.localPosition.y, GO[i].transform.localPosition.z);
if(GO[i].transform.localPosition.x < -4)
GO[i].transform.localPosition = new Vector3(8, GO[i].transform.localPosition.y, GO[i].transform.localPosition.z);
}
}
if ( resetClick ) isClicked = false; // Add this
A general note: Please make it a habit to mark the correct answers whenever you receive one. Cheers!
Would you please mark the answer as correct if it is so?
let me check if it is working or not... i think it will work.. but let me check first then i will mark it as correct