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 hjp309 · Aug 01, 2018 at 12:11 AM · networkingspawningclient-server

Server authoritative spawning bug?

I'm very new to UNET Networking so some insight into how I approached this would be greatly appreciated.

I'm trying to implement a server authoritative spawner that randomizes a prefab and a material before spawning them later into the scene. But for some reason from the client, I am seeing the right block spawn but with a different material (almost as if the number randomized again). I can't seem to wrap my head around why this is happening, but a guess would be that the values are not synchronized (SyncVar)? If that is the case, then why are my prefabs not random as well?

Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class BlockSpawnOnline : NetworkBehaviour
 {
     public GameObject smallBlock, regBlock, bigBlock, TBlock;
     public Material[] playerMaterials = new Material[4];
     public float spawnTime;
     
     int randomBlock;
     int randomMaterial;
 
     int maxBlocks = 10;
     float elapsed = 0;
     bool isSpawnBlocked;
 
     public List<GameObject> blocksToSpawn;
 
     public override void OnStartServer()
     {
         blocksToSpawn = new List<GameObject>();
 
         for (int i = 0; i < maxBlocks; i++)
         {
             randomBlock = Random.Range(0, 10);
             randomMaterial = Random.Range(0, 4);
 
             if (randomBlock <= 2)
             {
                 blocksToSpawn.Add(smallBlock.gameObject);
             }
             else if (randomBlock <= 5)
             {
                 blocksToSpawn.Add(regBlock.gameObject);
             }
             else if (randomBlock <= 7)
             {
                 blocksToSpawn.Add(bigBlock.gameObject);
             }
             else
             {
                 blocksToSpawn.Add(TBlock.gameObject);
             }
             blocksToSpawn[i].GetComponent<MeshRenderer>().material = playerMaterials[randomMaterial];
         }
     }
 
     void Update()
     {
         if (isServer)
         {
             if (isSpawnBlocked)
                 elapsed = 0;
             else
                 elapsed += Time.deltaTime;
             
             if (elapsed > spawnTime && maxBlocks > 0)
             {
                 Vector3 spawnPos = new Vector3(transform.position.x, transform.position.y + blocksToSpawn[maxBlocks-1].transform.localScale.y / 2, transform.position.z);
                 NetworkServer.Spawn(Instantiate(blocksToSpawn[maxBlocks - 1], spawnPos, transform.localRotation));
                 maxBlocks--;
                 elapsed = 0;
 
                 Debug.Log(this.name + " spawned " + blocksToSpawn[maxBlocks - 1].name + ", " + maxBlocks + " blocks left.");
             }
         }
     }
 
     void OnTriggerStay(Collider collision)
     {
         if (collision.gameObject.tag == "Block")
         {
             isSpawnBlocked = true;
             GetComponent<MeshRenderer>().material.SetColor("_Color", Color.red);
         }
     }
 
     void OnTriggerExit(Collider collision)
     {
         if (collision.gameObject.tag == "Block")
         {
             isSpawnBlocked = false;
             GetComponent<MeshRenderer>().material.SetColor("_Color", Color.yellow);
         }
     }
 }
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

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

Answer by hjp309 · Aug 07, 2018 at 12:17 AM

Never mind, I figured it out. I had to pass in the spawned object and the random number as parameters for my Rpc call after the object is spawned on the network, so that each client can see and grab the same object and change its material accordingly.


I feel like a lot of the change had more to do with deleting my playerMaterials array and just passing in each individual material publicly as I had done with my prefabs. I don't know why this fixed my problem, but I'm assuming the order of the materials somehow changed in the building process? If anyone can enlighten me on that, that would be great.

Fixed Code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;

 public class BlockSpawnOnline : NetworkBehaviour
 {
     public GameObject smallBlock, regBlock, bigBlock, longBlock;
     public Material playerOne, playerTwo, playerThree, playerFour;
     public float spawnTime;
     
     int randomBlock;
 
     int maxBlocks = 10;
     float elapsed = 0;
     bool isSpawnBlocked;
 
     public List<GameObject> blocksToSpawn;
 
     public override void OnStartServer()
     {
         blocksToSpawn = new List<GameObject>();
 
         for (int i = 0; i < maxBlocks; i++)
         {
             randomBlock = Random.Range(0, 10);
 
             if (randomBlock <= 2)
             {
                 blocksToSpawn.Add(smallBlock.gameObject);
             }
             else if (randomBlock <= 5)
             {
                 blocksToSpawn.Add(regBlock.gameObject);
             }
             else if (randomBlock <= 7)
             {
                 blocksToSpawn.Add(bigBlock.gameObject);
             }
             else
             {
                 blocksToSpawn.Add(longBlock.gameObject);
             }
         }
     }
 
     [ServerCallback]
     void Update()
     {
         if (isSpawnBlocked)
             elapsed = 0;
         else
             elapsed += Time.deltaTime;
             
         if (elapsed > spawnTime && maxBlocks > 0)
         {
             maxBlocks--;
             Vector3 spawnPos = new Vector3(transform.position.x, transform.position.y + blocksToSpawn[maxBlocks].transform.localScale.y / 2, transform.position.z);
             GameObject spawnedBlock = Instantiate(blocksToSpawn[maxBlocks], spawnPos, transform.localRotation);
             NetworkServer.Spawn(spawnedBlock);
             RpcChangeBlockMaterial(spawnedBlock, Random.Range(0, 4));
             elapsed = 0;
 
             //Debug.Log(this.name + " spawned " + blocksToSpawn[maxBlocks].name + ", " + maxBlocks + " blocks left.");
         }
     }
 
     [ClientRpc]
     void RpcChangeBlockMaterial(GameObject newBlock, int randomNum)
     {
         Debug.Log(randomNum);
         if (randomNum == 0)
             newBlock.GetComponent<MeshRenderer>().material = playerOne;
         if (randomNum == 1)
             newBlock.GetComponent<MeshRenderer>().material = playerTwo;
         if (randomNum == 2)
             newBlock.GetComponent<MeshRenderer>().material = playerThree;
         if (randomNum == 3)
             newBlock.GetComponent<MeshRenderer>().material = playerFour;
     }
 
     [ServerCallback]
     void OnTriggerStay(Collider collision)
     {
         isSpawnBlocked = false;
         if (collision.gameObject.tag == "Block")
         {
             isSpawnBlocked = true;
             GetComponent<MeshRenderer>().material.SetColor("_Color", Color.red);
         }
     }
     
     [ServerCallback]
     void OnTriggerExit(Collider collision)
     {
         if (collision.gameObject.tag == "Block")
         {
             isSpawnBlocked = false;
             GetComponent<MeshRenderer>().material.SetColor("_Color", Color.yellow);
         }
     }
 }
 
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

130 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

Related Questions

IS it possible to have delayed player spawning after server connection? (UNet) 1 Answer

Only host client is able to spawn objects 0 Answers

NetworkSpawn.Spawn() on all clients EXCEPT calling client 0 Answers

UNET Bug with NetworkHash128 and Saving Prefabs 0 Answers

UNET 5.2: How do you use NetworkServer.SpawnWithClientAuthority()? 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