Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Terrain.SetNeighbors is not working [minimal reproducible example attached]

Discussion in 'Scripting' started by gilley033, Apr 19, 2020.

  1. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    Please note, the Unity Package requires Unity 2018.4.12f1 or later

    I am banging my head against a wall here. I have two terrain in my scene (just as a test), one on the left and one on the right.

    The terrains are assigned to a left and right field on my script. Here is the code for setting their neighbors.

    Code (CSharp):
    1. void Start()
    2. {
    3.     left.SetNeighbors(null, null, right, null);
    4.     right.SetNeighbors(left, null, null, null);
    5. }
    upload_2020-4-19_16-1-37.png
    As you can see, the LODs do not matchup. The terrain heightmaps and alpha maps do match, which I can easily prove because if I enable "Auto Connect" on both terrains, everything works swimmingly.

    upload_2020-4-19_16-2-57.png
    I cannot fathom what the problem here is. I took a look at TerrainUtility.AutoConnect source code and all it does is call SetNeighbors for every terrain. I can only imagine that there is some other method I need to call before or after SetNeighbors to get this to work, yet I have seen others post that SetNeighbors alone should work.

    I have tried calling Terrain.SetConnectivityDirty, it doesn't help anything (pretty sure this is the method that just calls AutoConnect). I have also tried calling Terrain.Flush, it doesn't help anything.

    Has anyone ran into this issue? It seems like a bug but I have tried testing in 2018.4, 2019.1, and 2019.3 and it's a no go on all 3 versions, so I think not, unless there is something wrong with my project.

    And please do not recommend using Auto Connect. It is complete overkill for my use, as I am dynamically adding/removing terrains at runtime and some terrains in the scene don't need to have their neighbors reset. I want to target only the terrain that are effected by a terrain being added/removed.
     

    Attached Files:

    Last edited: Apr 22, 2020
  2. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    I don't know what to add to this. No one has come across this problem before?
     
  3. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    No promises that i will help (I have no necessary knowledge) but making and linking minimal reproducible example demonstrating the problem may attract someone willing to invest time into answering the question.

    Often (in 80% of cases?) making minimal reproducible example allows me to discover what was going wrong.
     
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    Thank you for your help!

    I've attached a Unity Package, just import it into any Unity 2018.4.12f1 or greater project, open up the neighbortest scene in Assets/Scenes, and hit play.

    Then, enable AutoConnect on both terrains and see how it should look.
     
  5. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    I would also edit the the title with "minimal reproducible example attached" (or just "minimal reproducible example" or "with MRE") to make more likely that people will visit the thread.
     
  6. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    Done. I attached the file to the original post and removed it from my last. Thanks!
     
  7. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    Please, anyone? If someone could just confirm that they are seeing the same behavior and this is not some bug with my particular project, that would be great!
     
  8. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    All right, I guess I'll submit this as a bug and see what comes of it.
     
  9. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    Bumping this once again. My bug report is open but I've gotten no response from the Unity team on it in a month, so I'm not sure where else to look for help/answers . . .
     
  10. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
  11. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    Update: This has been fixed in 2021.1 (at least, according to the Unity Issue Tracker)

    link

    Any chance this gets back ported? I would think it would be an easy fix, and unfortunately using AutoConnect is really bad when you have a lot of terrains in the scene.
     
  12. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,086
    So, this is strange, but Set Neighbors now works in the editor of 2020.2.3f1, but does not work in a build. I am going to test on 2019.4.23f1.

    Edit: I'm not sure if I had an error in my code or something else was going on, but it appears SetNeighbors does work in build as well, both in 2019 and 2020. I wonder if a dev could comment on whether the fix for 2021 was back ported to 2019 and 2020.

    However, there is a bug where SetNeighbors can only be called after frame 2, otherwise it does not work correctly. My guess is AutoConnect is called internally by Unity in Start, Update frame 1, or Update frame 2, which clears the neighbors of all terrains in the scene. This issue does not appear in 2021.

    To get around it, I have ensured that any SetNeighbors code runs after frame 2.
     
    Last edited: May 1, 2021