korimeily
korimeily6mo ago

How to disable and enable the prefab with normcore

Hi. I'm new to the multiplayer game. I'd like to disable and enable the prefab in my game while running. I'm using .SetActive() now. How do I use normcore? Thank you in advance for the answer.
Solution:
---------------------- If you want to remove the object at index 0 after finishing the loop, you can do so by calling the RemoveAt() method on the list. Here's how you can modify the previous example: ```csharp using UnityEngine;...
Jump to solution
54 Replies
kapa.ai
kapa.ai6mo ago
Hi @korimeily. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Normcore, you can use Realtime prefabs which are synchronized via the Realtime API using realtime views and realtime components. To disable and enable a prefab, you would typically use the SetActive() method in Unity. However, in a multiplayer setting with Normcore, you need to ensure that this change is synchronized across all clients. To do this, you can use the Realtime API's ownership and lifetime flags. These flags give you control over which clients can modify data in your rooms. For example, you can set a flag to denote that a certain client has the authority to enable or disable a prefab. Here's a basic example of how you might use these flags:
public void TogglePrefab() {
// Check if this client owns the prefab
if (realtimeView.isOwnedLocally) {
// Toggle the active state of the prefab
gameObject.SetActive(!gameObject.activeSelf);
}
}
public void TogglePrefab() {
// Check if this client owns the prefab
if (realtimeView.isOwnedLocally) {
// Toggle the active state of the prefab
gameObject.SetActive(!gameObject.activeSelf);
}
}
In this example, realtimeView is a reference to the RealtimeView component on your prefab. This component is what allows Normcore to synchronize the prefab across all clients. The isOwnedLocally property is a boolean that tells you whether the current client is the owner of the prefab. Remember, any changes you make to a prefab should be done through the Realtime API to ensure they are properly synchronized. For more detailed information, you can refer to the Realtime API documentation (https://normcore.io/documentation/realtime). Please note that this is a simplified example and your actual implementation may need to be more complex depending on your game's specific requirements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6mo ago
<@455563652175495174> kapa.ai is still learning and improving, please let us know how it did by reacting below
korimeily
korimeily6mo ago
This property is obsoleted. @Normcore Assistant
kapa.ai
kapa.ai6mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I apologize for the confusion. If the isOwnedLocally property is obsolete, you can use the isOwnedLocallySelf or isOwnedLocallyInHierarchy properties instead. These properties are part of the RealtimeView and RealtimeComponent classes in Normcore. isOwnedLocallySelf is a boolean that specifies whether the component is owned by the local client. isOwnedLocallyInHierarchy is a boolean that specifies whether the component or any of its parent views are owned by the local client. Here's how you might use these properties:
public void TogglePrefab() {
// Check if this client owns the prefab
if (realtimeView.isOwnedLocallySelf) {
// Toggle the active state of the prefab
gameObject.SetActive(!gameObject.activeSelf);
}
}
public void TogglePrefab() {
// Check if this client owns the prefab
if (realtimeView.isOwnedLocallySelf) {
// Toggle the active state of the prefab
gameObject.SetActive(!gameObject.activeSelf);
}
}
In this example, realtimeView is a reference to the RealtimeView component on your prefab. This component is what allows Normcore to synchronize the prefab across all clients. For more detailed information, you can refer to the RealtimeView documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeView) and the RealtimeComponent documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeComponent%601). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6mo ago
<@455563652175495174> kapa.ai is still learning and improving, please let us know how it did by reacting below
korimeily
korimeily6mo ago
I have a list that contains prefabs and I'd like to enable each prefab when I click the button. @Normcore Assistant
kapa.ai
kapa.ai6mo ago
Let me check the knowledge base for you. One moment! :books: Sorry @korimeily, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Mechabit
Mechabit6mo ago
easiest method is to make a custom model with a bool for set active. when the model changed event fires you set the game object active/ inactive. but for the view to be registered it needs to instantiate active so it might be better to set a child object as inactive if you need it to start hidden @Normcore Assistant how do i make a custom model with a bool property called setActive
kapa.ai
kapa.ai6mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To create a custom model with a bool property called setActive in Normcore, you would first define a class with the [RealtimeModel] attribute. Inside this class, you would define a private bool field with the [RealtimeProperty] attribute. Here's an example:
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class CustomModel {
[RealtimeProperty(1, true)]
private bool _setActive;
}
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class CustomModel {
[RealtimeProperty(1, true)]
private bool _setActive;
}
In this example, CustomModel is the name of your custom model. _setActive is the private field that will hold the value of your setActive property. The RealtimeProperty attribute tells Normcore to synchronize this field across all clients. The first argument to RealtimeProperty is the property ID, which must be unique within this model. The second argument specifies whether the property is reliable (i.e., whether changes to the property should always be sent to the server, even if some packets are lost). After defining your model, you would need to compile it. If you're using Unity 2019 or 2020, you can do this by going back to Unity, highlighting the CustomModel.cs file in your project, and clicking the "Compile Model" button in the inspector. If you're using Unity 2021 or newer, Normcore will automatically generate the remaining code needed to use your model when your project compiles. For more detailed information, you can refer to the Creating a custom realtime component (https://normcore.io/documentation/realtime/synchronizing-custom-data) and Creating a realtime model (https://normcore.io/documentation/realtime/synchronizing-custom-data#creating-a-realtime-model) sections of the Normcore documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6mo ago
<@162940911964258305> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit6mo ago
@Normcore Assistant how do i use that model to set my gameobject active when the model changes
kapa.ai
kapa.ai6mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To use the custom model to set your GameObject active when the model changes, you would need to create a RealtimeComponent subclass that uses your custom model. In this subclass, you would override the OnRealtimeModelReplaced() method to subscribe to changes to the setActive property. Here's an example:
using UnityEngine;
using Normal.Realtime;

public class CustomComponent : RealtimeComponent<CustomModel> {
protected override void OnRealtimeModelReplaced(CustomModel previousModel, CustomModel currentModel) {
if (previousModel != null) {
// Unsubscribe from events on the previous model
previousModel.setActiveDidChange -= SetActiveDidChange;
}

if (currentModel != null) {
// Subscribe to events on the new model
currentModel.setActiveDidChange += SetActiveDidChange;

// Set the initial active state of the GameObject
gameObject.SetActive(currentModel.setActive);
}
}

private void SetActiveDidChange(CustomModel model, bool value) {
// Update the active state of the GameObject when the model changes
gameObject.SetActive(value);
}
}
using UnityEngine;
using Normal.Realtime;

public class CustomComponent : RealtimeComponent<CustomModel> {
protected override void OnRealtimeModelReplaced(CustomModel previousModel, CustomModel currentModel) {
if (previousModel != null) {
// Unsubscribe from events on the previous model
previousModel.setActiveDidChange -= SetActiveDidChange;
}

if (currentModel != null) {
// Subscribe to events on the new model
currentModel.setActiveDidChange += SetActiveDidChange;

// Set the initial active state of the GameObject
gameObject.SetActive(currentModel.setActive);
}
}

private void SetActiveDidChange(CustomModel model, bool value) {
// Update the active state of the GameObject when the model changes
gameObject.SetActive(value);
}
}
In this example, CustomComponent is the name of your custom component. OnRealtimeModelReplaced() is a method that is called when the model for this component is replaced (e.g., when the component is first created or when switching rooms). setActiveDidChange is an event that is fired when the setActive property on the model changes. You would attach this CustomComponent script to the GameObject that you want to set active or inactive based on the setActive property of the model. For more detailed information, you can refer to the Creating a custom realtime component (https://normcore.io/documentation/realtime/synchronizing-custom-data) and Creating a realtime model (https://normcore.io/documentation/realtime/synchronizing-custom-data#creating-a-realtime-model) sections of the Normcore documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6mo ago
<@162940911964258305> kapa.ai is still learning and improving, please let us know how it did by reacting below
korimeily
korimeily6mo ago
I have a list that contains prefabs and I'd like to enable each prefab when I click the button. and when prefabs are active another player can see @Normcore Assistant
kapa.ai
kapa.ai6mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To enable each prefab in a list when a button is clicked and ensure that other players can see the changes, you can use Normcore's Realtime.Instantiate() method. This method instantiates a prefab and synchronizes it across all clients. Here's a basic example of how you might do this:
using UnityEngine;
using Normal.Realtime;

public class PrefabManager : MonoBehaviour {
[SerializeField] private Realtime _realtime;
[SerializeField] private List<GameObject> _prefabs;
[SerializeField] private Button _button;

private void Start() {
_button.onClick.AddListener(EnablePrefabs);
}

private void EnablePrefabs() {
foreach (GameObject prefab in _prefabs) {
// Instantiate a copy of the prefab and set it to be owned by us.
GameObject prefabInstance = Realtime.Instantiate(prefab.name, ownedByClient: true, useInstance: _realtime);

// Enable the prefab instance
prefabInstance.SetActive(true);
}
}
}
using UnityEngine;
using Normal.Realtime;

public class PrefabManager : MonoBehaviour {
[SerializeField] private Realtime _realtime;
[SerializeField] private List<GameObject> _prefabs;
[SerializeField] private Button _button;

private void Start() {
_button.onClick.AddListener(EnablePrefabs);
}

private void EnablePrefabs() {
foreach (GameObject prefab in _prefabs) {
// Instantiate a copy of the prefab and set it to be owned by us.
GameObject prefabInstance = Realtime.Instantiate(prefab.name, ownedByClient: true, useInstance: _realtime);

// Enable the prefab instance
prefabInstance.SetActive(true);
}
}
}
In this example, _realtime is a reference to the Realtime component in your scene, _prefabs is a list of the prefabs you want to enable, and _button is the button that will enable the prefabs when clicked. The EnablePrefabs() method is called when the button is clicked, and it iterates over each prefab in the list, instantiates it using Realtime.Instantiate(), and enables it using SetActive(true). Remember to add the prefabs to a Resources folder in your Unity project, as Normcore requires prefabs to be in a Resources folder to instantiate them at runtime. For more detailed information, you can refer to the Creating a Player Controller (https://normcore.io/documentation/guides/creating-a-player-controller#making-it-multiplayer​) and Creating a prefab (https://normcore.io/documentation/realtime#prefabs) sections of the Normcore documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------