- Home /
Question by
SneakiAssassin · Apr 15, 2021 at 10:42 PM ·
unity 5unityeditor
List shows as empty when function is called with OnClick()
When a button is pressed I want to change a variable in my script, but it didn't work, doing a little debugging I found that when the function is called in Update() the length of the list prints out as 4, but when called with the OnClick() editor thing during play it prints as length 0 (and I therefore can't change my variable because it relies on this list).
All code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EachPlanetScript : MonoBehaviour
{
public string planetName;
public string planetDescription;
GameObject planetDescriptionsObject;
int resourceAmount;
public List<string> resourceNames;
public string currentResourceNameText;
int currentResourceIndex;
string curResource;
void Start()
{
SetResourcesAmount();
GetPlanetResourceNames();
GetPlanetName();
GetPlanetDescription();
InitializePlanetTexts();
}
void Update()
{
currentResourceNameText = resourceNames[currentResourceIndex];
print(resourceNames.Count);
}
void GetPlanetResourceNames()
{
for (int i = 0; i < resourceAmount; i++)
{
do
{
curResource = PlanetDescriptions.resources[Random.Range(0, resourceAmount)];
} while (resourceNames.Contains(curResource));
resourceNames.Add(curResource);
}
}
string GetPlanetName()
{
if (PlanetDescriptions.planetNames.Count != 0)
{
int randNum1 = Random.Range(0, PlanetDescriptions.planetNames.Count);
planetName = PlanetDescriptions.planetNames[randNum1];
PlanetDescriptions.planetNames.RemoveAt(randNum1);
}
else
{
planetName = "REDACTED";
}
return planetName;
}
string GetPlanetDescription()
{
if (PlanetDescriptions.planetDescriptions.Count != 0)
{
int randNum2 = Random.Range(0, PlanetDescriptions.planetNames.Count);
planetDescription = PlanetDescriptions.planetDescriptions[randNum2];
PlanetDescriptions.planetDescriptions.RemoveAt(randNum2);
}
else
{
planetDescription = "PLANET INFO REDACTED";
}
return planetDescription;
}
void InitializePlanetTexts()
{
if (resourceNames.Count > 0)
{
currentResourceNameText = resourceNames[0];
currentResourceIndex = 0;
}
else currentResourceNameText = "";
}
public void UpdatePlanetTexts()
{
print(resourceNames.Count);
if ((currentResourceIndex+1 <= resourceNames.Count))
{
currentResourceIndex += 1;
}
else
{
currentResourceIndex = 3;
}
}
void SetResourcesAmount()
{
planetDescriptionsObject = GameObject.Find("PlanetDescriptions");
PlanetDescriptions planetDescriptions = planetDescriptionsObject.GetComponent<PlanetDescriptions>();
resourceAmount = planetDescriptions.planetResourceAmount;
if (resourceAmount > PlanetDescriptions.resources.Length) resourceAmount = PlanetDescriptions.resources.Length;
}
}
Relevant bits:
void Update()
{
currentResourceNameText = resourceNames[currentResourceIndex];
print(resourceNames.Count);
// prints 4
}
public void UpdatePlanetTexts()
{
print(resourceNames.Count);
// prints 0
if ((currentResourceIndex+1 <= resourceNames.Count))
{
currentResourceIndex += 1;
}
else
{
currentResourceIndex = 3;
}
}
Comment