Search Unity

Weird issue only with the "BUILT" game???

Discussion in 'Scripting' started by iSleepzZz, Jul 26, 2018.

  1. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Hey guys... So I have this script:

    Code (CSharp):
    1. public override void OnStartServer() {
    2.         base.OnStartServer();
    3.         GrowHarvestCoroutine(1);
    4. }
    5.  
    6. private Vector3 findGround(Vector3 pos) {
    7.         if (!groundMySpawn)
    8.             return new Vector3(pos.x + groundOffset.x, pos.y + groundOffset.y, pos.z + groundOffset.z);
    9.  
    10.         RaycastHit hit;
    11.         if (Physics.Raycast(pos, Vector3.down, out hit, 99)) {
    12.             if (hit.transform.gameObject.isStatic) {
    13.                 return new Vector3(hit.point.x + groundOffset.x, hit.point.y + groundOffset.y, hit.point.z + groundOffset.z);
    14.             }
    15.         }
    16.         return pos;
    17. }
    18.  
    19. public void GrowHarvestCoroutine(float _second)
    20.     {
    21.         StartCoroutine(GrowHarvest(_second));
    22.     }
    23.  
    24. IEnumerator GrowHarvest( float _second)
    25.     {
    26.         yield return new WaitForSeconds(_second);
    27.         HarvestableItem item = items[Random.Range(0, items.Length)];
    28.         Vector3 posToSpawnAt;
    29.      
    30.         if (!randomLocation)
    31.         {
    32.             posToSpawnAt = findGround(transform.position);
    33.         }
    34.         else
    35.         {
    36.             float radius = GetComponent<SphereCollider>().radius;
    37.             Vector3 originPoint = findGround(transform.position);
    38.             originPoint.x += Random.Range(-radius, radius);
    39.             originPoint.z += Random.Range(-radius, radius);
    40.  
    41.             posToSpawnAt = originPoint;
    42.         }
    43.  
    44.         var _item = Instantiate(item, posToSpawnAt, Quaternion.identity);
    45.         _item.area = this;
    46.  
    47.         NetworkServer.Spawn(_item.gameObject);
    48.      
    49.         //yield break;
    50.     }
    Which basically is spawning an object (a tree) on the multiplayer game. It also will spawn it directly on the ground with my "findGround" method.
    In playing the game in the editor, it works PERFECTLY!
    However, when I build the game, it doesn't work at all. It spawns it exactly where this gameobject is. Therefore not doing the whole, shoot a ray downwards and hit the terrain, and use that location instead.

    Why and how is this??? This seems very strange to me..........
     
  2. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Solved: Oddly enough, the server-side (in the built game) was saying that my Terrain was not static.... So I just changed the condition from
    Code (CSharp):
    1. hit.transform.gameObject.isStatic
    to
    Code (CSharp):
    1. hit.transform.name.Equals("Terrain")