I use netcode and I get error NotServerException: Only server can spawn NetworkObjects. It’s probably something like serverRpc but when I try it, error say ServerRpc method must end with ‘ServerRpc’ suffix!. Banana and Missile can’t spawn by client.
I don’t understand anything about this lol.
can someone help me pls? thank you!
code :
public class NewItemCollector : NetworkBehaviour
{
[SerializeField] private string items = { “SpeedBoost”, “Banana”, “EnergyDrink”, “Shield”, “Missile” };
private string currentItem;
public GameObject bananaPrefab;
public Transform bananaSpawnPosition;
private PlayerController pc;
private StaminaController sc;
[SerializeField] private Text itemText;
[SerializeField] private GameObject shieldPicture;
public float shieldTimer = 0f;
[SerializeField] private GameObject missilePrefab;
public Transform missileSpawnPosition;
private void Start()
{
pc = GetComponent<PlayerController>();
sc = GetComponent<StaminaController>();
shieldPicture.SetActive(false);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Item"))
{
// Get a random item from the items array
currentItem = items[Random.Range(0, items.Length)];
// Print the item that was collected
Debug.Log($"Collected {currentItem}");
// Display the name of the collected item on the screen
itemText.text = $"{currentItem}";
// Disable the item object so it can't be collected again
other.gameObject.SetActive(false);
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q) && !string.IsNullOrEmpty(currentItem))
{
// Use the currently held item
switch (currentItem)
{
case "SpeedBoost":
Debug.Log("Used SpeedBoost");
pc.SpeedBoost();
break;
case "Banana":
Debug.Log("Used Banana");
GameObject newBanana = Instantiate(bananaPrefab, bananaSpawnPosition.transform.position, transform.rotation);
NetworkObject networkObject = newBanana.GetComponent<NetworkObject>();
networkObject.Spawn();
break;
case "EnergyDrink":
Debug.Log("Used EnergyDrink");
sc.playerStamina = 100;
sc.UpdateStamina(1);
sc.sliderCanvasGroup.alpha = 0;
break;
case "Shield":
Debug.Log("Used Shield");
shieldTimer = 3f; // Set the shield timer to 3 seconds
Debug.Log("Used Shield");
shieldPicture.SetActive(true); // Activate the shield picture object
break;
case "Missile":
Debug.Log("Used Missile");
GameObject newMissile = Instantiate(missilePrefab, missileSpawnPosition.transform.position, transform.rotation);
NetworkObject newmissile = newMissile.GetComponent<NetworkObject>();
newmissile.Spawn();
break;
default:
Debug.LogError($"Unknown item: {currentItem}");
break;
}
// Clear the currently held item
currentItem = null;
// Clear the displayed item name
itemText.text = "";
}
if (shieldTimer > 0f)
{
shieldTimer -= Time.deltaTime;
if (shieldTimer <= 0f)
{
Debug.Log("Shield effect worn off");
shieldPicture.SetActive(false); // Deactivate the shield picture object
}
}
}
}