Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 20, 2020 at 02:14 PM by Rolo552 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Rolo552 · Mar 19, 2020 at 04:28 PM · script.gridreferencenullmissing

Deleted object tagged as missing instead of null.

Hello, I have a smaller question.


I have a cript that creates a grid made out of cubes.After the grid is created the game than randomly chooses some objects from a list and deletes them.


My problem is when I delete this object, the reference in the editor marks the object as missing. I understand that the object has reference to the neighbour but when it gets deleted the object canˇt find the its neighbour. My question is how could I avoid this or repair it ?

Here is my script.


Map generator public class MapGenerator : MonoBehaviour { public int mapSize; public int numberOfWaterHoles;

     public CubeCell cubeCell;
     public List<CubeCell> cubeCells = new List<CubeCell>();
 
     public bool landIsGenerated;
     public int i;
     public bool generateLand;
     void Start()
     {
         
         GenerateMap();
     }
 
     void GenerateMap() 
     {
         for (int x = 0 ; x < mapSize; x++)
         {
             for (int z = 0; z < mapSize; z++)
             {
                 CreateTile(x, z, i++);
             }
         }
 
         if (i == mapSize * mapSize) { landIsGenerated = true; } else { landIsGenerated = false; }
 
         if (landIsGenerated) 
         { 
             CreateWater();
         }
     }
 
     void CreateTile(int x,int z,int i) 
     {
         Vector3 tilePosition;
         tilePosition.x = x * 1f;
         tilePosition.y = 0;
         tilePosition.z = z * 1f;
 
         CubeCell cubeTile = Instantiate<CubeCell>(cubeCell);
   
         cubeCells.Add(cubeTile);
         cubeTile.transform.SetParent(transform, false);
         cubeTile.transform.localPosition = tilePosition;
 
         if (z > 0) { CubeCell.GetNeighbours_EastAndWest(cubeTile, cubeCells[i - 1]); }
         if (x > 0) { CubeCell.GetNeighbours_NorthAndSouth(cubeTile, cubeCells[i - mapSize]); }
         
     }
   void CreateWater() 
     {
         //Choose random number from a list delete the gameobject,empty space will be used to simulate water.Remaining objects will be used as land.
         
         for (int o = 0; o < numberOfWaterHoles; o++)
         {
 
             int randomNumber = Random.Range(o, (mapSize*mapSize));
 
             var randomObject = new CubeCell[numberOfWaterHoles];
             randomObject[o] = cubeCells[randomNumber];
 
             cubeCells[randomNumber] = cubeCells[o];
             cubeCells[o] = randomObject[o];
             cubeCells[o].DestroyObject(); 
         }        
     }    
 }



Cell script

 public class CubeCell : MonoBehaviour
 {
     [Header("Neighbours")]
     public CubeCell east, north, west, south;
 
     [Header("Land types")]
     public GameObject Grass_North;
     public GameObject Grass_South;
     public GameObject Grass_East;
     public GameObject Grass_West;
     public GameObject Grass_NorthEast;
     public GameObject Grass_NorthWest;
     public GameObject Grass_SouthEast;
     public GameObject Grass_SouthWest;
     public GameObject Grass_Middle;
 
     public GameObject currentLand;
 
 
     private void Start()
     {
 
             CreateLand();
       
            
     }
     public static void GetNeighbours_NorthAndSouth(CubeCell north, CubeCell south) 
     {
         north.west = south;
         south.east = north;
     }
 
     public static void GetNeighbours_EastAndWest(CubeCell east, CubeCell west)
     {
         east.south = west;
         west.north = east;
     }
 
     void CreateLand() 
     {
         Vector3 position = new Vector3(0,0,0);
 
         if(north == null && east == null) { currentLand = Grass_NorthEast; } else { currentLand = Grass_Middle; }
         GameObject newLand = Instantiate<GameObject>(currentLand);
         newLand.transform.localPosition = position;
         newLand.transform.SetParent(transform, false);
     }
 
     public void DestroyObject() 
     {
         GameObject.Destroy(gameObject);
     }
 }
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Zentiu · Mar 20, 2020 at 01:47 AM

You could remove the object you wish to delete from the list or remove or set to null any references other cells have first and then destroy the object? If an object gets destroyed, let the neighbours know that the object will be gone. Then you could destroy the object.

something like:

 public void DestroyObject() 
      {
          //go to all neighbours of this cell.
          //set the neighbours reference of this cell to null like: 'Tell neighbour east that object west is null'
          //also remove this cell from any list you might have for easy reference.
 
          //bombs away!
          GameObject.Destroy(gameObject);
      }

I hope this can be helpful. If not let me know.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rolo552 · Mar 20, 2020 at 02:14 PM 0
Share

That is actually a good idea.I havenˇt thought of this aproach. Thank you for your help.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException Error 6 Answers

The Variable has not been assigned. 1 Answer

How do I fix this Null Reference error and update my score? 2 Answers

script type variables 1 Answer

Using DestroyImmediate on one GameObject makes a reference to another a null 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges