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 *!= null)*
  •  	{*
    
  •  		if(isClicked)*
    
  •  		{*
    

GO.transform.localPosition = new Vector3(GO.transform.localPosition.x + avatarPosition, GO_.transform.localPosition.y, GO*.transform.localPosition.z);*_

* isClicked = false;*
* }*

_ if(GO*.transform.localPosition.x > 8)
GO.transform.localPosition = new Vector3(-4, GO.transform.localPosition.y, GO.transform.localPosition.z);*_

_ if(GO*.transform.localPosition.x < -4)
GO.transform.localPosition = new Vector3(8, GO.transform.localPosition.y, GO.transform.localPosition.z);
}
}
}*_

* void OnGUI()*
* {*
* for(int i=0; i<4; i++)*
* {*
_ if(GO*.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

1 Answer

1

within Update() change to the following:

  bool resetClick = false; // Add this
  for(int i=0 ; i<4; i++)
       {
         if(GO *!= null)*

{
if(isClicked)
{
GO.transform.localPosition = new Vector3(GO.transform.localPosition.x + avatarPosition, GO_.transform.localPosition.y, GO*.transform.localPosition.z);*_

resetClick = true; // Change this
}

if(GO*.transform.localPosition.x > 8)*
GO.transform.localPosition = new Vector3(-4, GO_.transform.localPosition.y, GO*.transform.localPosition.z);*_

if(GO*.transform.localPosition.x < -4)*
GO.transform.localPosition = new Vector3(8, GO_.transform.localPosition.y, GO*.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

it is still not working.... after the GO reaches 8 the GOs overlap with eachother. I think i am doing something wrong in these lines if(GO*.transform.localPosition.x > 8)* <em>GO<em>.transform.localPosition = new Vector3(-4, GO_.transform.localPosition.y, GO*.transform.localPosition.z);_</em></em> <em><em><em>_if(GO.transform.localPosition.x < -4)</em></em></em> <em><em><em><em><em>GO<em>.transform.localPosition = new Vector3(8, GO.transform.localPosition.y, GO.transform.localPosition.z);*_</em></em></em></em></em></em>

yeah it is working fine. Thanks for your reply