After Building and Run the game act different.

Hello, i am very new in programming, so from a lot of things, i am very confused of course, but most of them i am able to sort out by google out, but here I really don’t know. - C#

So, i somehow make my first game ever, try Build a game and when i did it, the game just start acting little bit weird. I just asking, if it’s coz i wrote it in so bad way/chaotic/newbie way, or what…

I maked Tower Defense, in this moment its only spawner, which spawning infity of waves of enemies and a lot of places, where i can build my towers. When enemy is spawned, it find the way, where to go (empty gameobjects), by find GameObjects and add them in array.

The problem is, in editor work everything absolutely okey, no errors nothing.
When i build the game, the enemies go instead of over 12waypoints over 2 and taking players life straight away.

Can just somebody tell me the logic of it, i mean, why it work in editor and out doesnt?
What keyword to looking for on google to learn this thing? Its some type of Architecture or something?

Thanks for help and sorry for my bad english, i just start do programming like month ago and this make me little bit sad.

You need to be more specific about the behavior. Show some screenshots of what it should be doing, and what it’s doing in a build.

Some of the most common things, though:

  • You’re doing things over time, but you’re not using Time.deltaTime, so you get different behavior depending on framerate. This affects things that have a “speed”, where those things will seem to go faster or slower in a build, or on different hardware.
  • Your UI is all messed up because you’ve configured it for one resolution, and it doesn’t work in a different resolution.

Anyway, show some screenshots, explain the issue, show the code that’s not working.

1 Like

Thank you so much for answer, here is more info, hope will help, coz i really wanna sort this out.
On the picture bellow, u can see my border. Enemies should spawn at beggining (left top) and they should go through all of this and on end, they should destroy them selves. But they destroy themself straight on second waypoint and they even not going straght. Here i posting even Enemy.cs script, because thats the place when enemy looking for path. Hope u will see there something else then mess and unexperience.



Here is the Image, i am not able to insert it here - Imgur: The magic of the Internet

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

public class Enemy : MonoBehaviour
{
    GameObject[] waypoints;
    int _waypoint = 0;
    GameObject lastParcel;
    public float hp = 30f;
    public float speed = 5f;
    public int hpTaking = 1;
    public int moneyGiving = 3;
 

    private void Start()
    {
        waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
        lastParcel = waypoints[waypoints.Length - 1].gameObject;
        transform.parent = GameObject.Find("SpawnedEnemy").transform;
    }

    private void Update()
    {
        Rotation();
        FollowPath();
        LastParcelDist();
        AliveCheck();
    }

    private void AliveCheck()
    {
        if (hp <= 0f)
        {
            Destroy(gameObject);
            GameObject.Find("Terrain").GetComponent<World>().playerMoney += moneyGiving;
        }
    }

    private void LastParcelDist()
    {
        float lastParcelDist = Vector3.Distance(lastParcel.transform.position, transform.position);

        if (lastParcelDist <= 2f)
        {
            Destroy(gameObject);
            GameObject.Find("Terrain").GetComponent<World>().playerHp -= hpTaking;
        }
    }

    private void FollowPath()
    {
        float dist = Vector3.Distance(transform.position, waypoints[_waypoint].transform.position);

        if (dist >= 0.5f)
        {
            transform.Translate(Vector3.forward * speed * Time.deltaTime);
        }
        else _waypoint++;
    }

    private void Rotation()
    {
        Vector3 dir = waypoints[_waypoint].transform.position - transform.position;
        Quaternion lookRotation = Quaternion.LookRotation(dir);
        Vector3 rotation = lookRotation.eulerAngles;
        transform.rotation = Quaternion.Euler(0f, rotation.y, 0f);
    }
}

The problem is probably this line:

waypoints = GameObject.FindGameObjectsWithTag("Waypoint");

You should not assume that the objects will be in a specific order. You’re probably getting them in reverse order in your build. I’d try to define your waypoints as an ordered array, not by finding the waypoints in the scene.

1 Like

Okey, so from what did u say, i guess easiest way what to do, when i arrive home, is just manualy put it in array and after that fallow the waypoints, right?
U have any idea how to do it properly? i mean only whats on my mind is for loop.
Thank you so much.

1 Like

An easy way would be to put the waypoints in a public array property on some class in your scene, and assign the waypoints in the inspector. Something like this:

5947700--637010--upload_2020-6-6_13-55-32.png

That class just has a public Transform[ ] Waypoints; property, and I assign the waypoints in the correct order in the inspector. This is fine when there aren’t too many waypoints.

Another approach is to put a component on each waypoint that defines its sort order, just an int value that defines the relative order of waypoints. Then, you can still use FindObjectsOfType, but then you’d order them by that value.

Yeah, the first way i ment. Thank you so much, it’s acting much better already.
So basically i need to make sure, unity exactly know, where is the point.
It’s need to be prefab? I don’t think so, no?

I don’t really understand your last question. But, the way I handle this kind of thing is to have each level store the waypoints, and then I assign those waypoints to any enemies that spawn in that level. So, one SceneController stores the waypoints, and that controller assigns them to the enemies that spawn.

Thank you so much, will do same, my English is very “funny”, sorry:smile:

Note that these types of issues are why you should do the majority of testing on builds instead of the editor. Use play mode in the editor to look at changes as you go, do actual testing in a build. My opinion of course, but it avoids these kinds of issues.

Yeah, this way definitely make sence, the thing is, i didnt had idea this can happen, just another experience, thank you so much guys!