JuliusSecret
JuliusSecret

Reputation: 149

C# - Unity crashes with this code - creating Transform array based on GameObject tag

I'm working in a 3D environment, a house, with 16 rooms. In each room I placed an invisible cube with a tag called "RoomsToScanTag". I'm working with an asset called Behavior Designer, and in my inspector I have given the tagname to look for (in this case "RoomsToScanTag"). With this tagname, I want the robot to go to room1 first, then continue to room2, up to room16.

This is my code

using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MoveTowardsNew : Action
{
    private Transform[] roomsToScanHAHA;
    public string targetTagRooms;
    public Transform targetyo;
    int i = 0;
    int controlNumber = 0;

    public override void OnAwake()
    {
        var targets = GameObject.FindGameObjectsWithTag (targetTagRooms);
        roomsToScanHAHA = new Transform[targets.Length];
        for (int i = 0; i < targets.Length; i++) {
            roomsToScanHAHA [i] = targets [i].transform;
        }
    }

    public override TaskStatus OnUpdate()
    {
        while (controlNumber < roomsToScanHAHA.Length)
        {
            targetyo = roomsToScanHAHA [controlNumber];
            if (Vector3.SqrMagnitude (transform.position - targetyo.position) < 0.5f)
            {
                if (controlNumber < roomsToScanHAHA.Length) {
                    controlNumber++;
                }
                return TaskStatus.Success;
            }
        }
        NavMeshAgent agent = GetComponent<NavMeshAgent> ();
        agent.destination = targetyo.position;
        return TaskStatus.Running;
    }
}

My problem

Unity crashes... It has something to do with this code, I can't figure it out. Anyone has an idea what is going wrong?

Here is an image of what I'm dealing with:

Example of setup in Behavior Designer

Thanks in advance!

Upvotes: 2

Views: 283

Answers (1)

Umair M
Umair M

Reputation: 10720

As usual, Its the while loop which is not terminating:

    while (controlNumber < roomsToScanHAHA.Length)
    {
        targetyo = roomsToScanHAHA [controlNumber];
        if (Vector3.SqrMagnitude (transform.position - targetyo.position) < 0.5f)
        {
            if (controlNumber < roomsToScanHAHA.Length) {
                controlNumber++;
            }
            return TaskStatus.Success;
        }
    }

First run: controlNumber = 0;

lets say your first element in array roomsToScanHAHA doesn't match the condition : (Vector3.SqrMagnitude (transform.position - targetyo.position) < 0.5f). controlNumber wont be incremented and would keep checking the condition for the first element. Which results in infinite loop.

So you can change your code to something like this:

    while (controlNumber < roomsToScanHAHA.Length)
    {
        targetyo = roomsToScanHAHA [controlNumber];
        if (Vector3.SqrMagnitude (transform.position - targetyo.position) < 0.5f)
        {
            return TaskStatus.Success;
        }
        controlNumber++;
    }

Hope this helps

Upvotes: 3

Related Questions