Search Unity

Neighbour terrains always null on execution

Discussion in 'Scripting' started by Darkgaze, Feb 6, 2020.

  1. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    I created neighbour terrains with the terains tool.

    After adding several of them, on my script I'm using the varaibles:

    Terrain myTerrain.topNeighbor
    Terrain myTerrain.bottomNeighbor...

    But all of those variables are null, even when there is a connection. How can that be possible? They all have the same GroupingID, reconnected, and when I draw over it, it seamesly paints from one to another.


    What is happening here?
     
  2. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    Here's a script I add to a center terrain with neighbours around it.
    Nothing is printed.

    This seems like a bug.

    Code (CSharp):
    1. var ter = GetComponent<Terrain>();
    2.  
    3.  
    4.         if (ter.topNeighbor != null)
    5.             Debug.Log( ter.topNeighbor.name);
    6.  
    7.  
    8.         if (ter.bottomNeighbor != null)
    9.             Debug.Log(ter.bottomNeighbor.name);
    10.  
    11.  
    12.         if (ter.rightNeighbor != null)
    13.             Debug.Log(ter.rightNeighbor.name);
    14.  
    15.  
    16.         if (ter.leftNeighbor != null)
    17.             Debug.Log(ter.leftNeighbor.name);
     
  3. ChrisTchou

    ChrisTchou

    Unity Technologies

    Joined:
    Apr 26, 2017
    Posts:
    74
    I just tested 2019.3.0f6, and it seems to work on my machine at least.

    One thing to check is if you add a Debug.Log("Terrain: " + ter.name) , does it print?

    If that prints and the other Debug.Logs don't print then there may be an issue of some kind;
    What version/platform are you using? Do you have a small repro project we can take a look at?

    Thanks!

    -Chris
     
  4. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    I uploaded a 250kb empty project with a simple setup. And yes, it prints the terrain name perfectly. But no neighbor variable is different from null.

    You can download it here.
    https://drive.google.com/file/d/1uZxHhutpdVB9vZUNWDuh3wweAlTj_qg-/view?usp=sharing

    Here is what I did:

    1. Create project
    2. Create a Terrain on the sample scene (create 3d object -> Terrain)
    3. Edit terrain by painting 3 more neighbours around it
    4. Add script (same code as before, adding the Debug.log you mentioned) on Start() and attach it to the center terrain (the first created).
    5. Play . Only the terrain name is printed.

    I'm using 2018.4.10f . I'm working on a long-term project and we can't move forward due to the Network capabilities that are removed. We are working on that.


    Thanks a lot
     
  5. ChrisTchou

    ChrisTchou

    Unity Technologies

    Joined:
    Apr 26, 2017
    Posts:
    74
    Ah, yeah, I see what is happening -- the terrains don't auto-connect themselves until around the 3rd frame.
    The auto-connect logic is tied into the culling pass; so that's probably why it is delayed.

    You can either work around it by grabbing the neighbors after a few frames have passed,
    or directly call TerrainUtility.AutoConnect() to force it to autoconnect on Start():

    https://docs.unity3d.com/2018.3/Doc...al.TerrainAPI.TerrainUtility.AutoConnect.html

    -Chris
     
  6. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    Oh. That's interesting. Thanks a lot. I'll try that.
     
  7. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    @ChrisTchou Thanks for your suggestion. If anybody is interested, on version 2018.4 it is done this way:
    UnityEngine.Experimental.TerrainAPI.TerrainUtility.AutoConnect();

    But I'm not allowed to use Experimental code on production, so I'll have to wait until it is connected.

    Thanks!
     
  8. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,906
    I've checked that while not in playmode, connection is reliable but in playmode it takes that time. Why? Can't it just autoconnect when the terrain initialize? Currently it's not robust as terrain tiles can be created at any time.