I have been working on a Project for almost half a year, I have had to create my own models so it has been slow progress. I am no expert when it comes to programming and my code is incredibly messy and generally plain awfully made. The more I code the more my game runs slow, I understand that happens when you have more code but I think there is certainly something I can do to help my game run better. Any tips? Or perhaps from my code below possibly tell me if you spot anything immediately wrong with my code?
Again thank you all in Advance!
Here is SOME of my code.
Again, I have been working on this for ages and this isnât even a half of it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Frey : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent Nav;
public GameObject yerF;
public GameObject playerBuildInterface;
public GameObject T1F1buildInterface;
public GameObject ResourceManager;
public GameObject EnergySystem;
public GameObject TheEnergie;
public GameObject TheEnerging;
public GameObject EnergyBar;
public GameObject newObject1;
public GameObject newObject2;
public GameObject newYerf;
public bool EnergyActive = false;
public bool TransferingEnergy = false;
public bool Collecting = false;
public bool ONce = false;
public bool isGd = false;
public bool isCd = false;
public bool stillStill = false;
public bool tt = false;
public float Ttimer = 1;
public float timer = 0;
//public bool Once = false;
// Start is called before the first frame update
void Start()
{
if (tt == false)
{
Nav = this.transform.parent.gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
}
playerBuildInterface = GameObject.FindWithTag("Xy");
T1F1buildInterface = GameObject.FindWithTag("Zy");
ResourceManager = GameObject.FindWithTag("ResourceManager");
EnergySystem = GameObject.FindWithTag("Wy");
EnergySystem = EnergySystem.transform.GetChild(0).gameObject;
newYerf = GameObject.FindWithTag("yerf");
EnergyBar = GameObject.FindWithTag("EnergyBar");
}
void Update()
{
Vector3 mouse = Input.mousePosition;
Ray castPoint = Camera.main.ScreenPointToRay(mouse);
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
if (TransferingEnergy == true && isGd == false)
{
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
{
if (hit.transform.tag != "Desert" && hit.transform.tag != "Untagged")
{
TheEnerging = hit.transform.gameObject;
TheEnergie = this.gameObject.transform.parent.gameObject;
isGd = true;
}
else
{
TransferingEnergy = false;
}
}
}
if (Collecting == true && isCd == false)
{
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
{
if (hit.transform.tag != "Desert" && hit.transform.tag != "Untagged" && hit.transform.tag == "Iron")
{
TheEnerging = hit.transform.gameObject;
TheEnergie = this.gameObject.transform.parent.gameObject;
isCd = true;
}
else
{
Collecting = false;
}
}
}
}
if (TransferingEnergy == true)
{
if (isGd == true)
{
if (ONce == false)
{
this.gameObject.transform.parent.gameObject.GetComponent<Energy>().BecomeStill = true;
Nav.destination = TheEnerging.transform.position;
transform.LookAt(TheEnerging.transform.position);
}
if (Input.GetMouseButtonDown(1) && TheEnergie.GetComponent<Energy>().BeingControlled == true)
{
TransferingEnergy = false;
}
if (Physics.Raycast(this.gameObject.transform.parent.gameObject.transform.position, transform.forward, out hit, 2))
{
if (hit.transform.gameObject.tag == TheEnerging.tag)
{
//stillStill = true;
if (TheEnerging == null)
{
TransferingEnergy = false;
}
if (TheEnergie == TheEnerging)
{
TransferingEnergy = false;
}
if (TheEnergie.GetComponent<Energy>().Enrgy > 0 && TheEnerging.GetComponent<Energy>().NeedEnergy == true)
{
if (TheEnergie.GetComponent<Energy>().isStill == false || TheEnerging.GetComponent<Energy>().isStill == false)
{
TransferingEnergy = false;
ONce = false;
Destroy(newObject1);
Destroy(newObject2);
}
if (ONce == true)
{
TheEnergie.GetComponent<Energy>().Enrgy -= Time.deltaTime * TheEnergie.GetComponent<Energy>().OutPutR;
TheEnerging.GetComponent<Energy>().Enrgy += Time.deltaTime * TheEnergie.GetComponent<Energy>().OutPutR;
}
else
{
ONce = true;
TheEnerging.GetComponent<PlaceHolder>().Frey = this.gameObject;
newObject1 = Instantiate(EnergyBar, TheEnergie.transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
newObject2 = Instantiate(EnergyBar, TheEnerging.transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
}
if (ONce == true)
{
newObject1.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().Decay = TheEnergie.GetComponent<Energy>().Enrgy;
newObject1.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().PrevDecay = TheEnergie.GetComponent<Energy>().MaxEnrgy;
//newObject1.GetComponent<LoadingBar>().Howmuch = 0.4f;
//newObject1.GetComponent<LoadingBar>().EnergyBar = true;
newObject2.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().Decay = TheEnerging.GetComponent<Energy>().Enrgy;
newObject2.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().PrevDecay = TheEnerging.GetComponent<Energy>().MaxEnrgy;
//w newObject2.GetComponent<LoadingBar>().Howmuch = 0.4f;
//newObject2.GetComponent<ScaleEngBar>().EnergyBar = true;
}
}
}
/*else
{
//if (stillStill == true)
//{
TransferingEnergy = false;
//stillStill = false;
TheEnerging = null;
TheEnergie = null;
//}
}
*/
}
}
}
else
{
isGd = false;
if (newObject1 != null)
{
newObject1.GetComponent<Look>().ded = true;
newObject2.GetComponent<Look>().ded = true;
Destroy(newObject1);
Destroy(newObject2);
ONce = false;
}
}
if (Collecting == true)
{
if (isCd == true)
{
if (ONce == false)
{
this.gameObject.transform.parent.gameObject.GetComponent<Energy>().BecomeStill = true;
Nav.destination = TheEnerging.transform.position;
transform.LookAt(TheEnerging.transform.position);
}
if (Physics.Raycast(this.gameObject.transform.parent.gameObject.transform.position, transform.forward, out hit, 2))
{
if (hit.transform.gameObject.tag == TheEnerging.tag)
{
//stillStill = true;
if (Input.GetMouseButtonDown(1) && TheEnergie.GetComponent<Energy>().BeingControlled == true)
{
Collecting = false;
}
if (TheEnerging == null)
{
Collecting = false;
}
if (TheEnergie == TheEnerging)
{
Collecting = false;
}
if (TheEnergie.GetComponent<Energy>().Enrgy > 0 && TheEnerging.GetComponent<Iron>().Durability > 0)
{
if (TheEnergie.GetComponent<Energy>().isStill == false || TheEnerging.GetComponent<Iron>().Durability == 0 || TheEnerging.GetComponent<Iron>().Durability < 0)
{
Collecting = false;
ONce = false;
Destroy(newObject1);
Destroy(newObject2);
}
TheEnergie.GetComponent<Energy>().Enrgy -= Time.deltaTime * TheEnergie.GetComponent<Energy>().Damage;
TheEnerging.GetComponent<Iron>().Durability -= Time.deltaTime * TheEnergie.GetComponent<Energy>().Damage;
if (TheEnerging.GetComponent<Iron>().Resource == "Iron")
{
timer += Time.deltaTime;
if (timer > Ttimer)
{
timer = 0;
int inter;
inter = Random.Range(-2, 2);
if (inter < 0)
{
inter = 0;
}
ResourceManager.GetComponent<ResourceManager>().Iron += inter;
}
}
if (ONce == false)
{
newObject1 = Instantiate(EnergyBar, TheEnergie.transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
newObject2 = Instantiate(EnergyBar, TheEnerging.transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
ONce = true;
Nav.destination = TheEnergie.transform.position;
TheEnerging.GetComponent<PlaceHolder>().Frey = this.gameObject;
}
if (ONce == true)
{
newObject1.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().Decay = TheEnergie.GetComponent<Energy>().Enrgy;
newObject1.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().PrevDecay = TheEnergie.GetComponent<Energy>().MaxEnrgy;
//newObject1.GetComponent<LoadingBar>().Howmuch = 0.4f;
//newObject1.GetComponent<LoadingBar>().EnergyBar = true;
newObject2.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().Decay = TheEnerging.GetComponent<Iron>().Durability;
newObject2.transform.GetChild(0).gameObject.GetComponent<ScaleEngBar>().PrevDecay = TheEnerging.GetComponent<Iron>().OriginalDurability;
//w newObject2.GetComponent<LoadingBar>().Howmuch = 0.4f;
//newObject2.GetComponent<ScaleEngBar>().EnergyBar = true;
}
}
}
/*else
{
//if (stillStill == true)
//{
TransferingEnergy = false;
//stillStill = false;
TheEnerging = null;
TheEnergie = null;
//}
}
*/
}
}
}
else
{
if (newObject1 != null)
{
if (isCd == true)
{
newObject1.GetComponent<Look>().ded = true;
newObject2.GetComponent<Look>().ded = true;
Destroy(newObject1);
Destroy(newObject2);
ONce = false;
}
}
isCd = false;
}
}
// Update is called once per frame
public void frey()
{
yerF.GetComponent<Teir1DroneFactory>().CreatingDroneT1 = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DroneT1F1 : MonoBehaviour
{
public bool BeingControlled = false;
public GameObject Player;
public float Energy = 100;
public float MaxEnergy = 100;
public float Speed = 2.5f;
public UnityEngine.AI.NavMeshAgent Nav;
public bool GivingEnergy = false;
public GameObject Frey;
public RaycastHit hit;
public float Damage = 1;
void Start()
{
Player = GameObject.FindWithTag("Player");
Nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
Upgraded();
Frey = this.transform.GetChild(0).gameObject;
//GameObject.FindWithTag("Frey");
}
void Upgraded()
{
Nav.speed = Speed;
GetComponent<Energy>().Damage = Damage;
}
void Update()
{
Vector3 mouse = Input.mousePosition;
Ray castPoint = Camera.main.ScreenPointToRay(mouse);
RaycastHit hit;
if (Input.GetKeyDown(KeyCode.Escape))
{
BeingControlled = false;
}
if (Input.GetMouseButtonDown(1) && BeingControlled == true)
{
GivingEnergy = false;
/*if (Energy < 0)
{
}*/
if (Energy > 1)
{
//GameObject newObject;
//newObject = Instantiate(DartThrower, new Vector3(mousePos.x / -42.952f, -30.945f, mousePos.z / -29.506f), Quaternion.identity) as GameObject;
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
{
Nav.destination = hit.point;
transform.LookAt(hit.point);
}
}
}
if (Energy > MaxEnergy)
{
Energy = MaxEnergy;
}
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
{
if (hit.transform.gameObject.tag == "Player")
{
BeingControlled = false;
}
}
if (BeingControlled == true)
{
GameObject.FindWithTag("yerf").GetComponent<Yerf>().yerf1 = this.gameObject.transform.GetChild(0).gameObject;
}
}
Frey.GetComponent<Frey>().EnergyActive = GivingEnergy;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Teir1DroneFactory : MonoBehaviour
{
public bool IsClicked = false;
public GameObject playerBuildInterface;
public GameObject T1F1buildInterface;
public GameObject DroneT1;
public GameObject ResourceManager;
public GameObject LoadingBar;
public bool CreatingDroneT1 = false;
public float DDD = 20;
public float Energy = 50;
public float MaxEnergy = 500;
public bool NeedEnergy = true;
public bool NoEnergy = false;
public bool ONce = false;
public GameObject Frey;
// Start is called before the first frame update
void Start()
{
Frey = GameObject.FindWithTag("Frey");
playerBuildInterface = GameObject.FindWithTag("Xy");
T1F1buildInterface = GameObject.FindWithTag("Zy");
T1F1buildInterface = T1F1buildInterface.transform.GetChild(0).gameObject;
ResourceManager = GameObject.FindWithTag("ResourceManager");
LoadingBar = GameObject.FindWithTag("LoadingBar");
LoadingBar.tag = "Finish";
LoadingBar.GetComponent<LoadingBar>().Decay = DDD;
LoadingBar.GetComponent<LoadingBar>().Howmuch = 1;
Energy = 50;
}
// Update is called once per frame
public void CreateThink()
{
if (ResourceManager.GetComponent<ResourceManager>().Iron > 99 )
{
CreatingDroneT1 = true;
}
else
{
CreatingDroneT1 = false;
}
}
void Update()
{
//this.GameObject.GetComponent<
if (Energy < MaxEnergy)
{
NeedEnergy = true;
}
else
{
NeedEnergy = false;
}
if (Input.GetKeyDown(KeyCode.Escape))
{
T1F1buildInterface.SetActive(false);
playerBuildInterface.SetActive(true);
}
if (Input.GetMouseButtonDown(0))
{
/*if (T1F1buildInterface.activeInHierarchy == true)
{
T1F1buildInterface.SetActive(false);
playerBuildInterface.SetActive(true);
}*/
//GameObject newObject;
//newObject = Instantiate(DartThrower, new Vector3(mousePos.x / -42.952f, -30.945f, mousePos.z / -29.506f), Quaternion.identity) as GameObject;
}
GameObject newObject;
if (CreatingDroneT1 == true)
{
if (ResourceManager.GetComponent<ResourceManager>().Iron > 99)
{
if (Energy > 0)
{
GetComponent<Energy>().Enrgy -= Time.deltaTime;
if (ONce == false)
{
newObject = Instantiate(LoadingBar, transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
ONce = true;
}
DDD -= Time.deltaTime;
if (DDD < 0.5f)
{
CreatingDroneT1 = false;
newObject = Instantiate(DroneT1, transform.position + new Vector3(4, 0, 4), Quaternion.identity) as GameObject;
DDD = 20;
ONce = false;
ResourceManager.GetComponent<ResourceManager>().Iron -= 100;
}
}
else
{
CreatingDroneT1 = false;
}
}
else
{
CreatingDroneT1 = false;
}
}
}
}
Again thank you all in Advance!