Anyone know how to fix this zombie script?

i have been working on a zombie game and i just started making a script that sends it to a random place but instead of doing that it just goes to 0,0,0 and stops working. Here is the script

using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.AI;

public class zombieAI : MonoBehaviour
{
    public NavMeshAgent agent;
    public Transform t;
    public float  walkRadius;
    public Vector3 currentPos;
    // Start is called before the first frame update
    void Start()
    {
       currentPos = transform.position;
     
       
 

        Vector3 newPos;
        newPos.x = UnityEngine.Random.Range(0.0f, 30.0f);
        newPos.y = currentPos.y;

        newPos.z = UnityEngine.Random.Range(0.0f, 30.0f);

        t.position = newPos;

        RaycastHit hit;
          if (Physics.Raycast(t.position, -Vector3.up, out hit, Mathf.Infinity))

      t.position = hit.point;
        agent.destination = t.position;





    }

    // Update is called once per frame
    void Update()
    {

      
    }
}

When you submit an issue like this it is always helpful to describe what you expected to happen, how/why you expected it to work and what happens instead. Do you already have any theories of what the issue could be?

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

If you find out more and it still seems mysterious, “stops working” is not a constructive way to report errors. Instead, try this format:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.

1 Like