1

While trying to create a game in Unity about you building a small city I stumbled upon a problem. I'm using NavMesh to have the player lead around tiny people (called mixxes here). The NavMesh is working, just I'm trying to make the mixxes produce different materials based on the location you lead them to.

Here is my code so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class MixxScript : MonoBehaviour
{
    public float speed = 5f;
    public Transform target = null;

    public Transform oldTarget = null;

    public Transform lastPossibleSuccess;

    public float range = 8.4f;
    public NavMeshAgent agent;
    private float countdown = 3;

    void Update()
    {
       if (target == null)
       {
            return;
       }
        agent.SetDestination(target.position);
        float distance = Vector3.Distance(transform.position, target.position);
        Debug.Log(distance);
        if (distance <= 2)
        {
            countdown -= Time.deltaTime;
            if (countdown <= 0)
            {
                if (target.transform.name == "Factory")
                {
                    MainScript.metal += 1;
                }
                if (target.transform.name == "Farm")
                {
                    MainScript.food += 1;
                }
                countdown = 3;
            }
        }
        else
        {
            countdown = 3;
        }

    }
    void OnMouseDown()
    {
        MainScript.selectedMixx = gameObject;
       
    }
}

And here is MainScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MainScript: MonoBehaviour
{

    public static GameObject selectedMixx = null;
    public static GameObject selectedPlatform = null;

    public static float metal = 0;
    public static float wood = 0;
    public static float money = 0;
    public static float mixxes = 1;
    public static float food = 50;

    public Text scoreText;

    void Update()
    {
        scoreText.text = "Money: $" + money.ToString() + "\nFood: " + food.ToString() + "\nMetal: " + metal.ToString() + "\nWood: " + wood.ToString() + "\nMixxes: " + mixxes.ToString();

    }


}

The problem is that 1) The text as shown in MainScript is not updating. 2) The Debug.Log() statement is not working so I cannot see what is wrong here.

What I've tried:

  • Multiple ways to measure distance
  • Different distance restraints (such as the one on line 28 of the first script)
  • Using FixedUpdate() instead of Update() (which failed completely)

Is there a solution to this?

  • So you got nothing in your logs ? are you sur "target" is not null ? – Orkad Feb 22 '21 at 07:03
  • Multiple things could go wrong here e.g. the name checks but if you don't even see the log I would join the comment before: `target` seems to be `null` .. have you tried [Debugging your code with breakpoints](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html)? – derHugo Feb 22 '21 at 20:29
  • You are both correct. Target is null because of a script in the background that I thought was unimportant to this question. Thanks a lot! – Rodion Zuban Feb 25 '21 at 20:58

0 Answers0