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 Vanderlolbroek · Apr 11, 2019 at 08:18 PM · networkingprogrammingrpcclient

rpc doesnt get called on client

I want to make an automated level generator for my multiplayer lan game and that's works al fine. But when I also want to build the level on the clients the RPC function only get called on the server. I have tried a lot of things but I don't know what I do wrong. So the problem is the PRC only gets called on the server and not on the clients. (and I know that Unet will be outdated but know a little bit how to use it and don't want to learn another networking solution)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class LevelGeneration : NetworkBehaviour
 {
     public GameObject plank;
     public GameObject building;
     public GameObject buildingBackground;
 
     private GameObject start;
     private GameObject end;
 
     [SyncVar]
     public Vector2 range;
     private Vector3 spawnPos;
 
     private float lastYPos;
 
     [SyncVar]
     private float randomPosY;
 
     [SyncVar]
     private float randomValueChooseHouse;
 
     [SyncVar]
     private float randomValueChooseBackgroundHouse;
 
     private bool tikTak;
 
     // Start is called before the first frame update
     void Start()
     {
         start = GameObject.FindGameObjectWithTag("Start");
         end = GameObject.FindGameObjectWithTag("End");
 
         if(isServer)
         {
             BuildMap();
         }
     }
 
     [ClientRpc]
     public void RpcPlaceBuildingOnClient(int i)
     {
         if (isServer)
         {
             return;
         }
         GameObject buildingObj = Instantiate(building, transform);
         buildingObj.transform.position = spawnPos;
 
         if (i == 0)
         {
             buildingObj.transform.position = new Vector3(buildingObj.transform.position.x, spawnPos.y - 2, buildingObj.transform.position.z);
         }
         else
         if (i == 9)
         {
             end.transform.position = buildingObj.transform.position;
         }
     }
 
     [ClientRpc]
     public void RpcPlacePlankOnClient(int i)
     {
         if (isServer)
         {
             return;
         }
         GameObject plankObj = Instantiate(plank, transform);
         plankObj.transform.position = spawnPos;
 
         if (i == 0)
         {
             plankObj.transform.position = new Vector3(plankObj.transform.position.x, spawnPos.y - 2, plankObj.transform.position.z);
         }
         else
         if (i == 9)
         {
             end.transform.position = plankObj.transform.position;
         }
     }
 
     public void BuildMap()
     {
         spawnPos = start.transform.position;
         for (int i = 0; i < 10; i++)
         {
             randomValueChooseHouse = Random.value;
 
             if (randomValueChooseHouse >= 0.7)
             {
                 //spawn Building and set on good location
                 GameObject buildingObj = Instantiate(building, transform);
                 buildingObj.transform.position = spawnPos;
                 RpcPlaceBuildingOnClient(i);
                 //doe iets als dit de eerste plank is en doe iets als dit laaste plank is
                 if (i == 0)
                 {
                     buildingObj.transform.position = new Vector3(buildingObj.transform.position.x, spawnPos.y - 2, buildingObj.transform.position.z);
                     if (isServer)
                     {
                         randomPosY = randomPosY - 2;
                     }
                 }
                 else
                 if (i == 9)
                 {
                     end.transform.position = buildingObj.transform.position;
                 }
             }
             else
             {
                 if (isServer)
                 {
                     randomValueChooseBackgroundHouse = Random.value;
                 }
 
                 //spawn plank and set on good location
                 GameObject plankObj = Instantiate(plank, transform);
                 plankObj.transform.position = spawnPos;
                 RpcPlacePlankOnClient(i);
                 //doe iets als dit de eerste plank is en doe iets als dit laaste plank is
                 if (i == 0)
                 {
                     plankObj.transform.position = new Vector3(plankObj.transform.position.x, spawnPos.y - 2, plankObj.transform.position.z);
                     if (isServer)
                     {
                         randomPosY = randomPosY - 2;
                     }
                 }
                 else
                 if (i == 9)
                 {
                     end.transform.position = plankObj.transform.position;
                 }
 
                 if (randomValueChooseBackgroundHouse > 0.5)
                 {
                     GameObject buildBackgroundObj = Instantiate(buildingBackground, transform);
                     if (tikTak)
                     {
                         buildBackgroundObj.transform.position = new Vector3(plankObj.transform.position.x + Random.Range(-6, 6), plankObj.transform.position.y + Random.Range(2, 15), plankObj.transform.position.z + 100);
                         tikTak = false;
                     }
                     else
                     {
                         buildBackgroundObj.transform.position = new Vector3(plankObj.transform.position.x + Random.Range(-6, 6), plankObj.transform.position.y + Random.Range(2, 15), plankObj.transform.position.z);
                         tikTak = true;
                     }
                 }
             }
 
             //zet spawn pos op een bepaalde plek
             lastYPos = randomPosY;
 
             if (isServer)
             {
                 randomPosY = Random.Range(range.x, range.y);
 
                 while (Mathf.Abs(lastYPos - randomPosY) > 2 && randomPosY >= lastYPos)
                 {
                     Debug.Log("hoi");
                     randomPosY = Random.Range(range.x, range.y);
                 }
 
             }
 
             spawnPos = new Vector3(spawnPos.x + 25, randomPosY, spawnPos.z);
             
         }
     }
 
 }
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

Answer by Vanderlolbroek · Apr 12, 2019 at 04:57 PM

I fixed it

the problem was that is started it before unet was done doing its thing. now I have fixed it and removed some bugs.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class LevelGeneration : NetworkBehaviour
 {
     public GameObject plank;
     public GameObject building;
     public GameObject buildingBackground;
 
     private GameObject start;
     private GameObject end;
 
     [SyncVar]
     public Vector2 range;
     private Vector3 spawnPos;
 
     private float lastYPos;
 
     [SyncVar]
     private float randomPosY;
 
     [SyncVar]
     private float randomValueChooseHouse;
 
     [SyncVar]
     private float randomValueChooseBackgroundHouse;
 
     private bool tikTak;
     private bool firstUpdate;
 
     // Start is called before the first frame update
     void Start()
     {
         start = GameObject.FindGameObjectWithTag("Start");
         end = GameObject.FindGameObjectWithTag("End");
         
     }
 
     private void Update()
     {
         if (isServer && firstUpdate == false)
         {
             BuildMap();
             firstUpdate = true;
         }
     }
 
     [ClientRpc]
     public void RpcPlaceBuildingOnClient(int i, Vector3 spawnPosClient)
     {
         if (isServer)
         {
             return;
         }
         GameObject buildingObj = Instantiate(building, transform);
         buildingObj.transform.position = spawnPosClient;
 
         if (i == 0)
         {
             buildingObj.transform.position = new Vector3(buildingObj.transform.position.x, spawnPosClient.y - 2, buildingObj.transform.position.z);
         }
         else
         if (i == 9)
         {
             end.transform.position = buildingObj.transform.position;
         }
     }
 
     [ClientRpc]
     public void RpcPlacePlankOnClient(int i, Vector3 spawnPosClient)
     {
         if (isServer)
         {
             return;
         }
         GameObject plankObj = Instantiate(plank, transform);
         plankObj.transform.position = spawnPosClient;
 
         if (i == 0)
         {
             plankObj.transform.position = new Vector3(plankObj.transform.position.x, spawnPosClient.y - 2, plankObj.transform.position.z);
         }
         else
         if (i == 9)
         {
             end.transform.position = plankObj.transform.position;
         }
     }
 
     [ClientRpc]
     public void RpcPlaceBackgroundBuildingClient(Vector3 spawnPosClient, float random1, float random2, bool tikTakClient)
     {
         if (isServer)
         {
             return;
         }
 
         GameObject buildBackgroundObj = Instantiate(buildingBackground, transform);
         if (tikTakClient)
         {
             buildBackgroundObj.transform.position = new Vector3(spawnPosClient.x + random1, spawnPosClient.y + random2, spawnPosClient.z + 100);
             tikTakClient = false;
         }
         else
         {
             buildBackgroundObj.transform.position = new Vector3(spawnPosClient.x + random1, spawnPosClient.y + random2, spawnPosClient.z);
             tikTakClient = true;
         }
     }
 
     public void BuildMap()
     {
         spawnPos = start.transform.position;
         for (int i = 0; i < 10; i++)
         {
             randomValueChooseHouse = Random.value;
 
             if (randomValueChooseHouse >= 0.7)
             {
                 //spawn Building and set on good location
                 GameObject buildingObj = Instantiate(building, transform);
                 buildingObj.transform.position = spawnPos;
                 RpcPlaceBuildingOnClient(i, spawnPos);
                 //doe iets als dit de eerste plank is en doe iets als dit laaste plank is
                 if (i == 0)
                 {
                     buildingObj.transform.position = new Vector3(buildingObj.transform.position.x, spawnPos.y - 2, buildingObj.transform.position.z);
                     randomPosY = randomPosY - 2;
                 }
                 else
                 if (i == 9)
                 {
                     end.transform.position = buildingObj.transform.position;
                 }
             }
             else
             {
                 randomValueChooseBackgroundHouse = Random.value;
 
                 //spawn plank and set on good location
                 GameObject plankObj = Instantiate(plank, transform);
                 plankObj.transform.position = spawnPos;
                 RpcPlacePlankOnClient(i, spawnPos);
                 //doe iets als dit de eerste plank is en doe iets als dit laaste plank is
                 if (i == 0)
                 {
                     plankObj.transform.position = new Vector3(plankObj.transform.position.x, spawnPos.y - 2, plankObj.transform.position.z);
                     randomPosY = randomPosY - 2;
                 }
                 else
                 if (i == 9)
                 {
                     end.transform.position = plankObj.transform.position;
                 }
 
                 if (randomValueChooseBackgroundHouse > 0.5)
                 {
                     float random1 = Random.Range(-6, 6);
                     float random2 = Random.Range(2, 15);
                     GameObject buildBackgroundObj = Instantiate(buildingBackground, transform);
                     if (tikTak)
                     {
                         buildBackgroundObj.transform.position = new Vector3(plankObj.transform.position.x + random1, plankObj.transform.position.y + random2, plankObj.transform.position.z + 100);
                         tikTak = false;
                     }
                     else
                     {
                         buildBackgroundObj.transform.position = new Vector3(plankObj.transform.position.x + random1, plankObj.transform.position.y + random2, plankObj.transform.position.z);
                         tikTak = true;
                     }
                     RpcPlaceBackgroundBuildingClient(plankObj.transform.position, random1, random2, tikTak);
                 }
             }
 
             //zet spawn pos op een bepaalde plek
             lastYPos = randomPosY;
             randomPosY = Random.Range(range.x, range.y);
 
             while (Mathf.Abs(lastYPos - randomPosY) > 2 && randomPosY >= lastYPos)
             {
                 randomPosY = Random.Range(range.x, range.y);
             }
 
 
             spawnPos = new Vector3(spawnPos.x + 25, randomPosY, spawnPos.z);
             
         }
     }
 
 }
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

153 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 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

Multiple Cars not working 1 Answer

Why is this not working properly? ClientRPC 0 Answers

Network RCP between client server projects 0 Answers

Force local client RPC to fire immediately? 0 Answers

clientrpc function gives null reference after client disconnects from host 0 Answers


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