Cant destroy object
I have a boss object that i instantiate with the following code:
But when i try to destroy it with a Realtime.Destroy i get the error in the attached image

44 Replies
Hi @✨Mike the Floof Wizard✨. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on your code and the error you're encountering when trying to destroy a boss object instantiated with Normcore, I can help troubleshoot this issue.
The error likely occurs because you're trying to destroy an object that wasn't properly instantiated with
Realtime.Instantiate()
or you're not referencing the object correctly when calling Realtime.Destroy()
.
Here are some potential solutions:
1. Make sure you're storing a reference to the instantiated boss object:
2. When destroying the object, use that reference:
Alternatively, you can destroy it using the RealtimeView component:
According to the Normcore documentation, Realtime.Destroy()
must be used instead of Unity's GameObject.Destroy()
for objects instantiated with Realtime.Instantiate()
Realtime Class Reference.
Also, make sure that:
- Your boss prefab has a RealtimeView component on its root GameObject
- The prefab is located in a Resources folder so it can be found by all clients
- You're not trying to destroy the object from a client that doesn't have ownership of it
If you're still encountering issues, you might need to check ownership of the object before destroying it:
This approach is recommended to prevent multiple clients from trying to destroy the same object simultaneously, which can cause errors like "Server attempted to delete value from set for key that doesn't exist in storage" Common Questions.
Unity: 2021.3.16f1
Normcore: 2.1
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant I am now spawning it correctly from one script. But when i try to destroy it in another script i get a nullreferenceexception
Let me check the knowledge base for you again. One moment! :books:
----------------------
I can see why you're getting a NullReferenceException in your BossHealth script. The issue is that you're trying to access
spawner.bossInstance
and spawner.bossBodyInstance
, but the spawner
variable is never initialized in your code.
Here are a few ways to fix this:
Option 1: Find the SpawnBoss reference
You need to initialize the spawner
variable in your Start method:
Option 2: Use the current GameObject
If you don't need to reference the spawner, you can simply destroy the current GameObject:
Option 3: Pass the reference when instantiating
A better approach would be to modify your SpawnBoss script to set a reference to the boss instance on the BossHealth component after instantiation:
Then in your BossHealth script:
Additional Notes
1. Since your BossHealth inherits from RealtimeComponent, you should be using the model to synchronize health across clients RealtimeComponent. Your commented code suggests you were working on this.
2. Make sure your BossHealth script is attached to a GameObject that has a RealtimeView component, and that the RealtimeView lists BossHealth in its components section RealtimeView.
3. If you're still having issues, check if your custom RealtimeComponent is properly syncing by ensuring OnRealtimeModelReplaced
is being called Synchronizing Custom Data.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant I tried using my commented code for managing the bosshealth but i am still getting the same nullreference exception
Let me check the knowledge base for you again. One moment! :books:
----------------------
I can see a few issues in your code that are likely causing the NullReferenceException:
Issue 1: Model Access
You're trying to access
model.currentHealth
and model.maxHealth
in your Start method, but the model might not be initialized yet. When using RealtimeComponents, the model is set by Realtime when you connect to the room, and it might not be available in Start().
Issue 2: SpawnBoss Component
You're trying to get the SpawnBoss component from the same GameObject that has the BossHealth component:
But it's likely that the SpawnBoss component is on a different GameObject, not on the boss itself.
Solution:
Here's how to fix these issues:
1. Use the OnRealtimeModelReplaced
method to initialize your model values:
2. Make sure your BossMoveModel has the necessary properties:
3. Use the SetSpawner method you've already created, but make sure it's called from the SpawnBoss script after instantiation:
4. Add a null check before trying to access the spawner:
According to the Normcore documentation, when using RealtimeComponents, you should wait for the model to be set before accessing it Common Questions. The model is set by Realtime when you connect to the room, and it might not be available in Start().
Also, make sure your BossHealth component is properly registered with a RealtimeView on your boss prefab Synchronizing Custom Data.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant i am still getting the same nullreference exception. At line 25 of my BossHealth script. Somehow the script cant find my bossInstance
Let me check the knowledge base for you again. One moment! :books:
----------------------
I can see that you're still having issues with the NullReferenceException at line 25 of your BossHealth script, which is likely the line where you're trying to access
spawner.bossInstance
.
The issue is that your spawner
variable is still null when you try to access it. Let's troubleshoot this step by step:
The Problem
Looking at your code, there are two possible causes for the NullReferenceException:
1. The spawner
variable is null
2. The spawner
variable is not null, but spawner.bossInstance
is null
Solutions
1. Verify the SetSpawner method is being called
Make sure your SpawnBoss script is actually calling the SetSpawner method after instantiating the boss:
2. Add null checks in your BossHealth script
Add thorough null checks to prevent the exception:
3. Alternative approach: Use direct GameObject reference
Instead of relying on the spawner reference, you could simply destroy the current GameObject:
4. Check for RealtimeView issues
According to the Normcore documentation, you might be encountering issues with your RealtimeComponent not being properly registered with a RealtimeView Synchronizing Custom Data.
Make sure:
- Your boss prefab has a RealtimeView component
- The BossHealth component is listed in the RealtimeView's components list
- If it's not listed, try removing and re-adding the BossHealth component
5. Debug with logging
Add debug logs to help identify where the issue is occurring:
These debugging steps should help you identify exactly where the issue is occurring and fix the NullReferenceException.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant My script is not finding the bosshealth component and i am also getting the error in the attached image

Let me check the knowledge base for you again. One moment! :books:
----------------------
I can see the issue in your SpawnBoss script. The problem is with the timing of when you're trying to access the BossHealth component.
The Issue
You're trying to get the BossHealth component from
bossBodyInstance
immediately after instantiating bossInstance
, but before bossBodyInstance
is actually created. Looking at your code:
1. You instantiate bossInstance
first
2. You start a coroutine to spawn bossBodyInstance
after a delay (waitTime
)
3. You immediately try to access bossHealth = bossBodyInstance.GetComponent<BossHealth>()
before the coroutine has completed
Since bossBodyInstance
is null at this point (it hasn't been instantiated yet), you get a NullReferenceException.
Solution
Move the BossHealth component check inside the coroutine, after bossBodyInstance
has been instantiated:
Additional Notes
1. According to the Normcore documentation, you should be careful when parenting objects with RealtimeViews. The documentation states: "We generally recommend that you don't [reparent]. Normcore will not synchronize parent relationships between objects." Common Questions
2. Make sure your BossHealth component is properly registered with a RealtimeView on your boss body prefab. If it's not showing up in the RealtimeView's components list, try removing and re-adding the BossHealth component Synchronizing Custom Data.
3. If you're still having issues, check if the BossHealth component is on the bossBodyInstance
prefab or if it's actually on the bossInstance
prefab. Your code assumes it's on the body, but it might be on the main boss object.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant The destroy works now, but my bossBodyInstance is an empty with 4 children attached to it. If i hit 1 of these children everything gets destroyed, how do i give each child a seperate health bar
Let me check the knowledge base for you again. One moment! :books:
----------------------
I understand you want to implement separate health bars for each child of your bossBodyInstance in your NormalVR project. Let's address this using Normcore's components.
Creating Separate Health for Children
You have a few options to implement separate health for each child of your bossBodyInstance:
Option 1: Add BossHealth Component to Each Child
You can add a separate BossHealth component to each child object:
Option 2: Create a Manager Script
Create a manager script on the parent that tracks the health of all children:
Then create a part-specific health script:
Important Considerations
1. RealtimeView Components: Each child object that has a health component needs its own RealtimeView component RealtimeView.
2. Model Registration: Make sure your custom RealtimeComponents are properly registered with their respective RealtimeViews. If you don't see your component in the RealtimeView's components list, try removing and re-adding it Synchronizing Custom Data.
3. Ownership: Be mindful of ownership when destroying objects. You may need to request ownership before destroying Ownership and Lifetime Flags.
4. Parent-Child Relationships: Remember that Normcore doesn't automatically synchronize parent-child relationships between objects with RealtimeViews. You'll need to handle this manually if needed.
5. Model Implementation: Don't forget to create the appropriate model classes (BossPartModel, BossPartsModel) with the necessary RealtimeProperty attributes for health values.
By implementing one of these approaches, you should be able to give each child of your bossBodyInstance its own health system while still maintaining the overall boss structure.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant I see you are using a BossPartsModel in the manager script. Can you show me what BossPartsModel looks like
Let me check the knowledge base for you again. One moment! :books:
----------------------
I'd be happy to show you what a BossPartsModel might look like for your NormalVR project. Here's an example implementation of the BossPartsModel class:
This model uses a RealtimeArray to track the IDs of all boss parts. The model also includes properties to track whether all parts are destroyed and optionally the total and remaining part counts.
After creating this class, you'll need to compile the model in Unity. The model compiler will generate the necessary code to synchronize this data across all clients.
For the individual boss parts, you would also need a BossPartModel:
Remember that when using RealtimeModels, you need to:
1. Add the
[RealtimeModel]
attribute to your class
2. Make the class partial
3. Add [RealtimeProperty]
attributes to each property you want to synchronize
4. Compile the model in Unity
This approach follows the pattern shown in the Synchronizing Custom Data documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant Which of these 2 models would i use for this script?
Let me check the knowledge base for you again. One moment! :books:
----------------------
For your BossPartsManager script, you would use the BossPartsModel that I showed you previously:
And for your BossPartHealth script, you would use the BossPartModel:
These models follow Normcore's RealtimeModel pattern where:
1. Each model is marked with the
[RealtimeModel]
attribute
2. Each property you want to synchronize is marked with [RealtimeProperty]
3. The classes are declared as partial
to allow Normcore to generate the networking code
Remember that after creating these model classes, you'll need to compile them in Unity. In Unity 2021+, this happens automatically. In older versions, you'll need to manually compile the models by highlighting the class and clicking "Compile Model" Room > RealtimeModel.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant I get an Duplicate 'RealtimeModel' attribute error on my BossPartModel
Let me check the knowledge base for you again. One moment! :books:
----------------------
I can help you with that duplicate 'RealtimeModel' attribute error. This is a common issue when working with Normcore models.
The error occurs when you have the
[RealtimeModel]
attribute applied twice to the same class. This typically happens in one of these scenarios:
1. You've manually added the attribute twice in your code
2. You're using Unity 2021+ where Normcore automatically generates model code, but you're also trying to manually compile the model
Here's how to fix it:
1. First, make sure you only have the [RealtimeModel]
attribute once at the top of your BossPartModel class:
2. If you're using Unity 2021+, Normcore automatically generates the model code for you. You don't need to manually compile the model or add any autogenerated code sections Synchronizing Custom Data.
3. If you're using Unity 2019/2020, make sure you're not adding the autogenerated code manually. The model compiler will add it for you when you click "Compile Model" in the inspector Synchronizing Custom Data.
4. Check if you have any duplicate model files in your project. Sometimes this happens if you've copied a script and forgotten to rename it.
After making these changes, try recompiling your project. If you're still having issues, you might need to restart Unity to clear any cached compilation errors.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant my bossParts model gets the following error:
The type 'int' cannot be used as type parameter 'TValue' in the generic type or method 'RealtimeArray<TValue>'. There is no boxing conversion from 'int' to 'Normal.Realtime.Serialization.IModel'.
Let me check the knowledge base for you again. One moment! :books:
----------------------
I can see the issue with your BossPartsModel. The error occurs because RealtimeArray can only store RealtimeModel objects, not primitive types like integers.
According to the Normcore documentation, RealtimeArray is "a special model type that represents an unordered collection of models" RealtimeArray<TValue>. This means you can't directly store primitive types like
int
in a RealtimeArray.
Here's how to fix your BossPartsModel:
Option 1: Create a wrapper model for your integers
Option 2: Use a different collection type
If you just need to track which parts are alive/dead, you could use a different approach:
Then in your BossPartsManager script, you can just track the parts locally and update the model properties:
This approach is simpler and avoids the need for a RealtimeArray altogether.
Remember that according to the Normcore documentation on Supported Primitives, only specific types can be used directly in RealtimeModels, and collections like RealtimeArray can only contain other RealtimeModel objects.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant Im getting the error in the attached image on line 28 of the following script:

Let me check the knowledge base for you again. One moment! :books:
----------------------
I can help you with this error. Based on the error occurring on line 28 of your PartHealth script, it's likely related to accessing
model.currentHealth
in your IsAlive() method.
The issue is that your model might be null when IsAlive() is called. This commonly happens with RealtimeComponents in Normcore when the model hasn't been initialized yet or has been destroyed.
Here's how to fix it:
This issue is common with RealtimeComponents in Normcore. According to the documentation, the model is set by Realtime when you connect to the room, and it might not be available immediately Common Questions.
Also, make sure your PartModel class is properly defined:
And ensure your PartHealth component is properly registered with a RealtimeView on your game object. If it's not showing up in the RealtimeView's components list, try removing and re-adding the PartHealth component Synchronizing Custom Data.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant i am still getting the same error on line 28
Let me check the knowledge base for you again. One moment! :books:
----------------------
I understand you're still getting the same error on line 28 of your PartHealth script. Let's take a closer look at what might be causing this persistent issue.
Line 28 is in your
IsAlive()
method where you're checking model.currentHealth > 0
. The error suggests that either:
1. The model
is null, or
2. The currentHealth
property doesn't exist or can't be accessed
Let's try a more comprehensive approach:
1. First, add more detailed debugging to pinpoint exactly what's happening:
2. Make sure your PartModel is correctly defined and compiled:
3. Check if your RealtimeView is properly set up:
- Ensure the PartHealth component is listed in the RealtimeView's components list
- Try removing and re-adding the PartHealth component to the RealtimeView
4. Verify that OnRealtimeModelReplaced
is being called by adding a debug log:
According to the Normcore documentation, if your custom RealtimeComponent isn't syncing and OnRealtimeModelReplaced doesn't get called, it's likely because the component isn't properly registered with the RealtimeView Synchronizing Custom Data.
If you're still experiencing issues after these steps, you might need to temporarily modify your code to avoid the error while you troubleshoot:
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant parthealth is reaching 0, but i still get this error on the Realtime.Destroy function

Let me check the knowledge base for you again. One moment! :books:
----------------------
I understand you're still having issues with the
Realtime.Destroy()
function in your PartHealth script even though the health is reaching 0. This is a common issue with Normcore's object destruction.
The error is likely related to ownership. In Normcore, only the owner of an object can destroy it. According to the documentation, when trying to destroy an object, you should check ownership first:
This approach is recommended in the Normcore documentation to prevent errors when multiple clients try to destroy the same object Common Questions.
If you're still encountering issues after implementing this ownership check, you might need to add more debugging to understand exactly what's happening:
Remember that according to the Normcore documentation on networked physics, ownership is a key concept for controlling objects in a multiplayer environment Networked Physics.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant i am still getting this error:

Let me check the knowledge base for you again. One moment! :books:
----------------------
I understand you're still encountering an error with your Normcore implementation. Without seeing the specific error message in your latest question, I'll need to make some assumptions based on our previous conversation.
It seems you're still having issues with the
Realtime.Destroy()
function in your PartHealth script. This is likely related to ownership issues in Normcore.
Here are some additional troubleshooting steps you can try:
1. Make sure you're checking ownership before destroying objects:
2. Check if the object has a valid RealtimeView component. According to the Realtime Class Reference, you can only use Realtime.Destroy()
with GameObjects that were instantiated with Realtime.Instantiate()
.
3. Add more detailed error logging to identify exactly what's happening:
4. Make sure your RealtimeView hierarchy is properly set up. If a parent object owns a child object, you might need to request ownership of the parent first.
If you could share the specific error message you're receiving, I could provide more targeted assistance for your NormalVR implementation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@Normcore Assistant how do i request ownership from a parent object
Let me check the knowledge base for you again. One moment! :books:
----------------------
To request ownership from a parent object in Normcore, you need to understand how ownership hierarchy works in the RealtimeView system.
According to the Normcore documentation, ownership settings of a parent are enforced on all children. If you want to request ownership of a child object, you need to make sure the parent is either unowned or owned by you first.
Here's how to request ownership from a parent object:
Basic Approach
Using the Root View
If you need to request ownership of the entire hierarchy, you can use the
GetRootView()
method:
Requesting Ownership of Self and Children
Normcore also provides a convenient method to request ownership of a view and all its children:
According to the RealtimeView documentation, these methods are available to help manage ownership within a hierarchy of objects.
Remember that ownership is enforced by the server, so if a parent is owned by another client, your request might be rejected. In that case, you'll need to coordinate with the other client to release ownership first.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@372679700885012485> Kapa.ai is still learning and improving, please let me know how I did by reacting below.