world streaming not working correctly

I’m trying to have it so only the terrain tiles close to the player are active to cut on resource usage and so my computer doesn’t crash. For some reason, my code doesn’t work 100% as intended. I start with this, character at selescted


and end up with this, with these distance values

I don’t understand why it deactivated the tile the player stands on even though it recorded the distance correctly and it is under the required amount?

This is the code being used

public class HideTiles : MonoBehaviour
{
[SerializeField]
private string tileTag;

[SerializeField]
private Vector3 tileSize;

[SerializeField]
private int maxDistance;

private GameObject[ ] tiles;

public float[ ] dist = new float[16];

// Use this for initialization
void Start()
{
this.tiles = GameObject.FindGameObjectsWithTag(tileTag);
DeactivateDistantTiles();
}

void DeactivateDistantTiles()
{
Vector3 playerPosition = this.gameObject.transform.position;
int num = 0;

foreach (GameObject tile in tiles)
{
Vector3 tilePosition = tile.gameObject.transform.position; //+ (tileSize / 2f);

float xDistance = Mathf.Abs(tilePosition.x - playerPosition.x);
float zDistance = Mathf.Abs(tilePosition.z - playerPosition.z);

float distance = Mathf.Sqrt((xDistance * xDistance) + (zDistance * zDistance));

dist[num] = distance; //getting the distance into the inspector
num += 1; //
if (distance > maxDistance)
{
tile.SetActive(false);
}
else
{
tile.SetActive(true);
}
}
}

void Update()
{
DeactivateDistantTiles();
}
}

What’s wrong here?

Using unity 2019.3.11

Please use code tags and check you don’t have another instance of this scripting running on another object in your scene.

If you type t:HideTIles in the Hierarchy search bar, that should show you if you do.

  • It is indeed the only instance in the scene.
  • I reworked whats called to get the distance, and made sure it gets the actual spot to check, middle of tile instead of bottom left corner.
  • It seems to work for everything but the bottom row and the second from the bottom on the right, they just refuse to appear no matter what.
  • I made some text to show the distance since reading from the inspector array became too complicated. Each text bit has reference to the checker and the tile under it, I then compied and pasted my code from the checker to have it get distance. For some god forsaken reason the check and text scripts return diferent calculated values despite being copied and pasted code
  • Sorry about the given code. It appeared to still be indeted before hitting post.
  • U can give more details later

There’s two things: when you paste code, please use the Paste-And-Match-Style feature instead, because otherwise sometimes the indenting gets munched. But the other thing is to add code tags, as outlined here: https://discussions.unity.com/t/481379

As for your actual problem, I recommend placing ONE tile in the scene, then you have only one piece of data to reason about, and drag your player around in the inspector window and make sure the “too far away” and “close enough to be visible” calculations for that one tile are correct. Don’t try to do it with more than one tile until you have one tile working perfectly.

I seem to have somewhat narrowed down the problem. the distance checker does work, calculates the distance and the hierarchy shows the object becomes active at the appropriate time. The issue is that while the hierarchy shows it’s active it remains invisible. Also, I don’t know if its GAIA acting up again but if bring in their player character the tiles that had been refusing to become visible suddenly do, however only if the character is within range even though it doesn’t have a script to do so, the terrains becoming invisible or not depends on characters position and if my checker makes the tile active. if the character gets too far the terrain become invisible but still active

It can be any number of things. Most likely you made a mistake when you calculate if you need to disable and/or enable a tile. Also it is possible that you properly hide the tile but then somewhere else you make it visible… We don’t know you didn’t share your relevant code.

public class HideTiles : MonoBehaviour
{
    [SerializeField]
    private string tileTag;

    [SerializeField]
    private int tileSize;

    [SerializeField]
    private int maxDistance;

    public GameObject[] tiles;
   
    //show distacne calculated in inspector
    public float[] dist = new float[16];

    // Use this for initialization
    void Start()
    {
        this.tiles = GameObject.FindGameObjectsWithTag(tileTag);
        DeactivateDistantTiles();
    }


    void DeactivateDistantTiles()
    {
        Vector3 playerPosition = this.gameObject.transform.position;
        int num = 0;

        foreach (GameObject tile in tiles)
        {
            Vector3 tilePosition = tile.gameObject.transform.position;

            //get midle of tile instead of bottom left corner
            tilePosition.x += 1024 / 2;
            tilePosition.z += 1024 / 2;
            tilePosition.y = playerPosition.y;

            float distance = Vector3.Distance(tilePosition, playerPosition);

           dist[num] = distance;
            num += 1;
            if (distance > maxDistance)
            {
                tile.SetActive(false);
            }
            else
            {
                tile.SetActive(true);
            }
        }
    }

    void Update()
    {
        DeactivateDistantTiles();
    }
}