Hey, i have a problem with the script i am writing for a game. i want GameObjects that i saved in an array to move along the x-axis smoothly to a destination, what is the best way to implement it, every post i saw about this is using update function for the gameobject, this is not working for me tho. Is there a better way?
This is the only script for this game:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class Rigi : MonoBehaviour
{
public GameObject[] Akkus, FarbigKraftwerk, FailureLight;
public Button Generator, button1shadow, button2shadow, button1symbol, button2symbol, button1cable, button2cable;
public GameObject symbol, shadow, cable;
int count1, countshadow, countsymbol, countcable, countfailure;
public Sprite[] spriteSymbols, spriteShadows, spriteCables, LightBulbs;
public Dictionary<GameObject, List<Sprite>> AkkuDic = new Dictionary<GameObject, List<Sprite>>();
public void On_Click_Button1()
{
List<Sprite> ActualList ;
List<Sprite> CompareList = new List<Sprite>();
CompareList.Add(spriteSymbols[countsymbol]);
CompareList.Add(spriteShadows[countshadow]);
CompareList.Add(spriteCables[countcable]);
AkkuDic.TryGetValue(Akkus[count1], out ActualList);
if (ActualList.SequenceEqual(CompareList))
{
if (count1+1 == Akkus.Length)
{
FarbigKraftwerk[count1].GetComponent<BoxCollider2D>().enabled = true;
Rigidbody2D gameObjectsRigidBody = FarbigKraftwerk[count1].AddComponent<Rigidbody2D>();
gameObjectsRigidBody.mass = 50;
StartCoroutine(AttachCoroutine());
StartCoroutine(MoveLastCoroutine());
}
if (count1+1 < Akkus.Length)
{
FarbigKraftwerk[count1].GetComponent<BoxCollider2D>().enabled = true;
Rigidbody2D gameObjectsRigidBody = FarbigKraftwerk[count1].AddComponent<Rigidbody2D>();
gameObjectsRigidBody.mass = 50;
StartCoroutine(AttachCoroutine());
StartCoroutine(MoveCoroutine());
}
if (count1+1 > Akkus.Length)
{
}
}
else
{
if (countfailure == FailureLight.Length)
{
}
else
{
FailureLight[countfailure].GetComponent<SpriteRenderer>().sprite = LightBulbs[1];
countfailure++;
}
}
}
public void Awake()
{
List<Sprite> mySpritesWind = new List<Sprite>();
mySpritesWind.Add(spriteSymbols[5]);
mySpritesWind.Add(spriteShadows[0]);
mySpritesWind.Add(spriteCables[1]);
List<Sprite> mySpritesOel = new List<Sprite>();
mySpritesOel.Add(spriteSymbols[4]);
mySpritesOel.Add(spriteShadows[2]);
mySpritesOel.Add(spriteCables[0]);
List<Sprite> mySpritesWasser = new List<Sprite>();
mySpritesWasser.Add(spriteSymbols[0]);
mySpritesWasser.Add(spriteShadows[3]);
mySpritesWasser.Add(spriteCables[1]);
List<Sprite> mySpritesSolar = new List<Sprite>();
mySpritesSolar.Add(spriteSymbols[3]);
mySpritesSolar.Add(spriteShadows[4]);
mySpritesSolar.Add(spriteCables[1]);
List<Sprite> mySpritesKern = new List<Sprite>();
mySpritesKern.Add(spriteSymbols[1]);
mySpritesKern.Add(spriteShadows[1]);
mySpritesKern.Add(spriteCables[0]);
List<Sprite> mySpritesKohle = new List<Sprite>();
mySpritesKohle.Add(spriteSymbols[2]);
mySpritesKohle.Add(spriteShadows[5]);
mySpritesKohle.Add(spriteCables[0]);
AkkuDic.Add(Akkus[0], mySpritesWind);
AkkuDic.Add(Akkus[1], mySpritesOel);
AkkuDic.Add(Akkus[2], mySpritesWasser);
AkkuDic.Add(Akkus[3], mySpritesSolar);
AkkuDic.Add(Akkus[4], mySpritesKern);
AkkuDic.Add(Akkus[5], mySpritesKohle);
Akkus[count1].transform.position += new Vector3(-22, 0, 0);
}
public void On_Click_Button1shadow()
{
countshadow++;
if (countshadow == spriteShadows.Length)
{
countshadow = 0;
}
shadow.GetComponent<SpriteRenderer>().sprite = spriteShadows[countshadow];
}
public void On_Click_Button2shadow()
{
if (countshadow == 0)
{
countshadow = spriteShadows.Length;
}
countshadow--;
shadow.GetComponent<SpriteRenderer>().sprite = spriteShadows[countshadow];
}
public void On_Click_Button1symbol()
{
countsymbol++;
if (countsymbol == spriteSymbols.Length)
{
countsymbol = 0;
}
symbol.GetComponent<SpriteRenderer>().sprite = spriteSymbols[countsymbol];
}
public void On_Click_Button2symbol()
{
if (countsymbol == 0)
{
countsymbol = spriteSymbols.Length;
}
countsymbol--;
symbol.GetComponent<SpriteRenderer>().sprite = spriteSymbols[countsymbol];
}
public void On_Click_Button1cable()
{
countcable++;
if (countcable == spriteCables.Length)
{
countcable = 0;
}
cable.GetComponent<SpriteRenderer>().sprite = spriteCables[countcable];
}
public void On_Click_Button2cable()
{
if (countcable == 0)
{
countcable = spriteCables.Length;
}
countcable--;
cable.GetComponent<SpriteRenderer>().sprite = spriteCables[countcable];
}
IEnumerator AttachCoroutine()
{
yield return new WaitForSeconds(4);
FarbigKraftwerk[count1].GetComponent<Rigidbody2D>().isKinematic = true;
FarbigKraftwerk[count1].transform.parent = Akkus[count1].transform;
}
IEnumerator MoveCoroutine()
{
yield return new WaitForSeconds(8);
Akkus[count1].transform.position += new Vector3(-22, 0, 0);
Akkus[count1 + 1].transform.position += new Vector3(-22, 0, 0);
count1++;
}
IEnumerator MoveLastCoroutine()
{
yield return new WaitForSeconds(8);
Akkus[count1].transform.position += new Vector3(-22, 0, 0);
count1++;
}
}