- Home /
In game log not working.
Hi i have a human gameObject with a script attached to it. I run it , no errors but nothing works. However, if I remove lines 70 to 78 it works fine, but does not get a name. I do not know what is going on. Script:`using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class humanAI : MonoBehaviour {
public bool Alive = true;
public bool Happy = true;
public int happiness;
public int Hunger;
public bool male;
public int Thirst;
public int Health;
public int age;
public int energy;
public string Name;
public List<string> MaleNames;
public List<string> FemaleNames;
System.Random rand;
public Text Log;
public bool CanBreed = false;
void NAMES()
{
MaleNames = new List<string>()
{
"Bob",
"Garry",
"Max",
"Arnold",
"Jeff",
"Stewart",
"Isaac",
"Hamish",
"Alby",
"Lachlan",
"Luke",
"Dylan",
"Guy"
};
FemaleNames = new List<string>()
{
"Sarah",
"Sienna",
"Sunny",
"Skye",
"Chloe",
"Joseph",
"Zia",
"Zarah",
"Edye",
"Amber",
"Daphne",
"Lilly",
"Crystal"
};
}
// Use this for initialization
void Awake () {
male = (Random.value > 0.5f);
Hunger = 1050;
Alive = true;
Thirst = 1000;
Health = 100;
happiness = 100;
energy = 100;
//works fine if i remove here
if (male == true)
{
Name = MaleNames[Random.Range(0, MaleNames.Count - 1)];
}
else
{
Name = FemaleNames[Random.Range(0, FemaleNames.Count - 1)];
}
// to here
if (male == true)
{
Log.text +="A male named " + Name + " was born." + "\n";
}
if (male == false)
{
Log.text += "A female named " + Name + " was born." + "\n";
}
}
// Update is called once per frame
void Update () {
if (Alive == true)
{
if (age >= 300)
{
CanBreed = true;
}
if (age >= 3000)
{
Die(" died of old age.");
}
else
{
age += 1;
}
if (Hunger == 0)
{
Die(" died of hunger.");
}
else
{
Hunger -= 1;
}
if (Thirst == 0)
{
Die(" died of dehydration.");
}
else
{
Thirst -= 1;
}
if (Happy == false && happiness == 0)
{
Die(" died of depression.");
}
if (Happy == false && Health > 0)
{
happiness -= 1;
}
if (Health == 0)
{
{
Die(" died of injuries.");
}
}
if (energy == 0)
{
Die(" died of exhaustion.");
}
}
}
public void Die(string DeathMessage)
{
Log.text = Log.text + Name + DeathMessage + "\n";
Alive = false;
}
} `
Answer by Blue-Cut · Nov 13, 2017 at 04:05 PM
Hello,
I am not sure why no error is shown but... there is somthing weird with your code. I don't see where you actually initialize your list of names.
You have a method called NAMES()
that build the lists. But you don't call this function in Awake()
, before setting the Name with Name =
.
Try to call NAMES()
before your if/else block that set the name and see what happens :
void Awake () {
NAMES();
Name = MaleNames[Random.Range(0, MaleNames.Count - 1)];
Debug.Log(Name);
}
Answer by bhavinbhai2707 · Nov 13, 2017 at 04:28 PM
Well i don't think so if it will show you anything in Debug!! because you are not calling the Names() Function anywhere!! Try Calling it first in Awake()!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612142754im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Need help calling a variable from another C# script 1 Answer
Multiple Cars not working 1 Answer
stupid errors i can't figure out 1 Answer
Argument out of range. 1 Answer