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 /
avatar image
0
Question by bificommander · Oct 03, 2019 at 01:55 PM · instantiatemissingreferenceexception

MissingReferenceException on other instances after destroying one.

I have some trouble with my code that lets you launch and dock a fleet from a space station. When I launch, I instantiate a fleet object from a prefab, this object has the PlayerFleetOnMap script, which inherits from FleetOnMapBase. When a fleet tries to dock, that fleet object should be destroyed. This works, but from that moment on all other fleets that were launched or are launched later give MissingReferenceException errors when accessing their child objects.

Both the objects and their child objects are still there in the Hierarchy, and in the inspector I can even see that the references to those child objects (assigned as variables on the PlayerFleetOnMap script) are still there and pointing to the correct objects. But breakpoints in the script show that, within the running PlayerFleetOnMap script, those variables have values of "null" (including those quotation marks).

I've looked at questions from people with similar problems, and most seemed to be overwriting their prefab when instantiating, but as far as I can see that's not happening here:

     public bool TryLaunchFleet(List<VesselInfo> vessels, out FleetOnMapBase fleetOnMap)
     {
         fleetOnMap = null;
         FleetInfo newFleet = dockedFleet.SplitFleet(vessels);
         newFleet.isDocked = false;
         newFleet.faction = faction;
 
         var fleetOnMapInstance = Instantiate(fleetOnMapPrefab, transform.position, Quaternion.identity);
         fleetOnMap = fleetOnMapInstance.GetComponent<FleetOnMapBase>();
 
         return fleetOnMap.TrySetFleetInfo(newFleet, this);
 
     }
 
     public virtual void DockFleet(FleetOnMapBase fleetOnMap)
     {
         dockedFleet.MergeFleet(fleetOnMap.fleetInfo);
         Destroy(fleetOnMap.gameObject);
     }

If instead of destroying I use SetActive(false) no problems occur, but the fleet object will stay in the hierarchy. I could try object pooling, but first I want to know what I'm doing wrong here to prevent other problems later on. Can anyone see what the mistake is?

Comment
Add comment · Show 3
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 Bonfire-Boy · Oct 03, 2019 at 04:09 PM 0
Share

I don't see anything obviously wrong in what you've posted so far. But you say the error happens when the children are accessed and it doesn't look like you've shown us that.

Does the $$anonymous$$ergeFleet function move children from one fleet object to another? If so then my guess would be that the issue is something to do with how that works. Like, if you were to do it by copying references from fleet A to fleet B but not reparenting them, and then destroying the parent, that could cause the kind of thing you're describing.

avatar image bificommander Bonfire-Boy · Oct 04, 2019 at 11:34 AM 0
Share

I don't think that's the problem. The $$anonymous$$ergeFleet transfers VesselInfo's (non-$$anonymous$$onobehavior classes that are basically lists of stats) from one FleetInfo (dito) to the other:

     public void $$anonymous$$ergeFleet(FleetInfo fleetToBe$$anonymous$$erged)
     {
         foreach(var vessel in fleetToBe$$anonymous$$erged.vesselsInFleet)
         {
             AddVessel(vessel);
         }
         fleetToBe$$anonymous$$erged.vesselsInFleet.Clear();
         PersistentFleet$$anonymous$$anager.allFleets.Remove(fleetToBe$$anonymous$$erged);
     }


These info-classes don't correspond to any gameObjects The children that give the error are child objects in the Hierarchy that are exclusive to the physical objects in the scene. It happens after TryLaunchFleet is called, when the launched fleet is set as the selected fleet to give orders to:

     public override void SetSelected(bool selected)
     {
         isSelected = selected;
         fleetRangeIndicator.SetActive(isSelected);
         selectedIndicator.enabled = isSelected;
         selectionColorIndex = isSelected ? ColorUtill.targetColorSelectedIndex.selected : ColorUtill.targetColorSelectedIndex.notSelected;
         SetTargetLineColor();
     }

The error happens with fleetRangeIndicator.SetActive(isSelected), although when calling SetTargetLineColor it would also give an error when accessing the LineRenderer object.

avatar image bificommander Bonfire-Boy · Oct 04, 2019 at 01:17 PM 0
Share

Woops, nvm, I figured out my mistake.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bificommander · Oct 04, 2019 at 01:22 PM

Okay, it was just a stupid mistake on my part: There wasn't a problem with the other instances at all. It's just that when selecting another fleet, or creating one which results in it automatically being selected, it would first try to deselect the previously selected fleet. If that previous fleet was the docked and destroyed fleet it would, quite reasonably, result in the above errors. I didn't see it because I use SetSelected(bool selected) as the same method for both selecting and deselecting a fleet, and I assumed the error occured when selecting a new fleet.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 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

Related Questions

MissingReferenceException: The object of type 'laser' has been destroyed but you are still trying to access it. 0 Answers

[Resolved: Had to remove messenger listeners]When Instantiating a prefab, after a previous clone of said prefab has been destroyed, I get a "MissingReferenceException". 0 Answers

When the player dies and I reload the scene the enemies spawn script stops working. 2 Answers

Pass a copy of a GameObject as variable to another script? 1 Answer

Checking if object intersects? 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