this.name not working in Start
Hi. I'm trying to get a prefab renamed to "Villager" when it's instantiated. This method has worked for other instantiated prefabs, but for some reason it's not renaming when I run this particular script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Villager : MonoBehaviour {
public float walkSpeed;
public float runSpeed = 6f;
private bool isWalking;
private bool isRunning;
private bool Work;
private bool Daytime;
void Start () {
StartCoroutine(Idle());
this.name = "Villager";
Work = false;
Daytime = GameObject.Find("Game Control").GetComponent<DayCycle>().Daytime;
}
void Update () {
if (Work == false)
{
transform.Translate(Vector3.forward * walkSpeed * Time.deltaTime);
Debug.Log(TentGoal.name[2]);
}
//TentList = new List<Tents>;
}
//If villager has nothing to do
IEnumerator Idle()
{
yield return new WaitForSeconds(Random.Range(1, 4));
isWalking = false;
transform.Rotate(new Vector3(0, Random.Range(0f, 360f), 0));
yield return new WaitForSeconds(Random.Range(1, 4));
isWalking = true;
StartCoroutine(Idle());
}
void LateUpdate()
{
if (isWalking == true)
{
walkSpeed = 2f;
}
else if (isWalking == false)
{
walkSpeed = 0f;
}
}
}
I'm not sure if this is because I started a Coroutine before I try to rename, but any help would be appreciated. Everything else in the script runs as I need.
Thanks.
Comment
Answer by rich66 · Nov 20, 2017 at 08:49 PM
Found the issue. User Error! The tag had a typo. Thanks for your help!