Question by
BrandonKirkley · Sep 15, 2019 at 09:09 PM ·
listoptimizationefficiencyoptimizeefficient
How to optimize this extremely repetitive list script?
Hi! I've made a script that has a list and checks what numbers are in what place inside of it. If a certain number is in a certain slot, it activates a gameobject assigned to that number, and the placement depends on which slot it is.
The script works, but it is horribly inefficient right now. I've put in 4 gameobjects but I plan to add far more, and at this rate it would take way too much time.
I am just a beginner coder; I was wondering, how do I make this code less bulky? I can tell there's a LOT of repetition in it, but I don't know how to make it.....not repetitive.
If someone could give me a tip, I would appreciate it so much! Thank you for reading. Here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class numberlist : MonoBehaviour
{
public static float THESEARETHENECESSARYSTARTINGFILLERSFORTHESLOTS;
private Vector3 THESEARETHECOORDINATESOFSLOTONE = new Vector3(180, 700, 0);
private Vector3 THESEARETHECOORDINATESOFSLOTTWO = new Vector3(520, 700, 0);
private Vector3 THESEARETHECOORDINATESOFSLOTTHREE = new Vector3(850, 700, 0);
public static float THISISTHELITERALSLOTONE;
public static float THISISTHELITERALSLOTTWO;
public static float THISISTHELITERALSLOTTHREE;
public static GameObject ALICEstaysuplateoften;
public static GameObject ALICElikestheearlymorning;
public static GameObject PETERisscaredofsylvia;
public static GameObject PETERisscaredofeverything;
public static List<float> THISISTHEACTUALLIST = new List<float>();
void Start()
{
THESEARETHENECESSARYSTARTINGFILLERSFORTHESLOTS = 0;
THISISTHEACTUALLIST.Add(THESEARETHENECESSARYSTARTINGFILLERSFORTHESLOTS);
THISISTHEACTUALLIST.Add(THESEARETHENECESSARYSTARTINGFILLERSFORTHESLOTS);
THISISTHEACTUALLIST.Add(THESEARETHENECESSARYSTARTINGFILLERSFORTHESLOTS);
ALICEstaysuplateoften = GameObject.Find("ALICE stay up late");
ALICEstaysuplateoften.SetActive(false);
ALICElikestheearlymorning = GameObject.Find("ALICE sunrise birds");
ALICElikestheearlymorning.SetActive(false);
PETERisscaredofsylvia = GameObject.Find("PETER is scared of sylvia");
PETERisscaredofsylvia.SetActive(false);
PETERisscaredofeverything = GameObject.Find("PETER is scared of everything");
PETERisscaredofeverything.SetActive(false);
}
// Update is called once per frame
void Update()
{
THISISTHELITERALSLOTONE = THISISTHEACTUALLIST[0];
THISISTHELITERALSLOTTWO = THISISTHEACTUALLIST[1];
THISISTHELITERALSLOTTHREE = THISISTHEACTUALLIST[2];
if (THISISTHEACTUALLIST[0] == 1)
{
ALICEstaysuplateoften.SetActive(true);
ALICEstaysuplateoften.transform.position = THESEARETHECOORDINATESOFSLOTONE;
}
if (THISISTHEACTUALLIST[1] == 1)
{
ALICEstaysuplateoften.SetActive(true);
ALICEstaysuplateoften.transform.position = THESEARETHECOORDINATESOFSLOTTWO;
}
if (THISISTHEACTUALLIST[2] == 1)
{
ALICEstaysuplateoften.SetActive(true);
ALICEstaysuplateoften.transform.position = THESEARETHECOORDINATESOFSLOTTHREE;
}
if (THISISTHEACTUALLIST[0] == 2)
{
ALICElikestheearlymorning.SetActive(true);
ALICElikestheearlymorning.transform.position = THESEARETHECOORDINATESOFSLOTONE;
}
if (THISISTHEACTUALLIST[1] == 2)
{
ALICElikestheearlymorning.SetActive(true);
ALICElikestheearlymorning.transform.position = THESEARETHECOORDINATESOFSLOTTWO;
}
if (THISISTHEACTUALLIST[2] == 2)
{
ALICElikestheearlymorning.SetActive(true);
ALICElikestheearlymorning.transform.position = THESEARETHECOORDINATESOFSLOTTHREE;
}
if (THISISTHEACTUALLIST[0] == 3)
{
PETERisscaredofsylvia.SetActive(true);
PETERisscaredofsylvia.transform.position = THESEARETHECOORDINATESOFSLOTONE;
}
if (THISISTHEACTUALLIST[1] == 3)
{
PETERisscaredofsylvia.SetActive(true);
PETERisscaredofsylvia.transform.position = THESEARETHECOORDINATESOFSLOTTWO;
}
if (THISISTHEACTUALLIST[2] == 3)
{
PETERisscaredofsylvia.SetActive(true);
PETERisscaredofsylvia.transform.position = THESEARETHECOORDINATESOFSLOTTHREE;
}
if (THISISTHEACTUALLIST[0] == 4)
{
PETERisscaredofeverything.SetActive(true);
PETERisscaredofeverything.transform.position = THESEARETHECOORDINATESOFSLOTONE;
}
if (THISISTHEACTUALLIST[1] == 4)
{
PETERisscaredofeverything.SetActive(true);
PETERisscaredofeverything.transform.position = THESEARETHECOORDINATESOFSLOTTWO;
}
if (THISISTHEACTUALLIST[2] == 4)
{
PETERisscaredofeverything.SetActive(true);
PETERisscaredofeverything.transform.position = THESEARETHECOORDINATESOFSLOTTHREE;
}
}
}
Comment