Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 brandonhotdo · Jul 19, 2014 at 02:42 PM · networkingrandomphotonrpcsync

RPC with Random Numbers!

Here is my code using UnityEngine; using System.Collections;

 public class StarCreate : Photon.MonoBehaviour {
     public GameObject prefab;
     public int StarCount;
 
     // Use this for initialization
 
     void Start () {
         GetComponent<PhotonView> ().RPC (
             "UniCreate",
             PhotonTargets.All);
 
 
     }
     [RPC]
     void UniCreate (){
         StarCount = Random.Range (100, 200);
         int i = 0;
         while (i < StarCount) {
             Instantiate(prefab, new Vector3 (Random.Range (-10000,100000), Random.Range (-10000,100000),Random.Range (-100000,100000)), Quaternion.identity);
             i++;
         }
     }

so I was wondering how you sync Random Numbers over the network though rpc! The problem is that every client gets the prefab instantiated in a different place! I want the Prefab to be instantiated in a random area but the same for every client though! That's all thanks for the help (BTW I did search the web for solutions to this but could not find any similar to my problem, so I I'm not that lazy XD).

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

2 Replies

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

Answer by Owen-Reynolds · Jul 19, 2014 at 08:21 PM

Roll the dice in Start, before you make the RPC, and send the Vector3 result. In other words, one person is responsible for the randomness. Everyone else just gets an RPC "make a new thing at this position."

Comment
Add comment · Show 3 · 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
avatar image zigglr · Dec 05, 2015 at 08:56 PM 0
Share

How can I send the actual random number variable to all of the other players?

avatar image Owen-Reynolds · Dec 05, 2015 at 10:15 PM 0
Share

ziggle: Thinking of a "random number variable" isn't really helpful here.

Once you roll a random number, and put it in a variable, it's just a regular variable with a regular value. You can send it like anything else.

Now, if you're thinking of a way to send a "dice" variable, which will automatically roll the same numbers as the first "dice" variable, that's possible using seeds (look up random number seeds.) But it's more complicated and I don't think as reliable (they can get out-of-synch if you aren't careful.)

avatar image zigglr Owen-Reynolds · Dec 05, 2015 at 10:22 PM 0
Share

Sorry, what I mean is I would like player 1 to make a random int, and then send this as an int variable to all other players, who can then use that variable. Just not sure what the code would like like for this. Thanks

avatar image
0

Answer by Runalotski · Dec 06, 2015 at 05:34 PM

You would add an argument to the method that you pass a value to

  public class StarCreate : Photon.MonoBehaviour {
      public GameObject prefab;
      public int StarCount;
  
      // Use this for initialization
  
      void Start () {
 
          //I create a random Number
             float rnd = Random.Range(0.0f, 100.0f);
 
          //Rpc has thee parts to the method (name of RPC, Targets, Arguments[])
          GetComponent<PhotonView> ().RPC (
              "UniCreate",
              PhotonTargets.All,
              rnd); //now the rnd value is sent to the rpc
  
  
      }
      [RPC]
      //Here I make an argument called rnd of type float
      void UniCreate (float rnd){
 
            //rnd will now be the same for all clients
       
          StarCount = Random.Range (100, 200);
          int i = 0;
          while (i < StarCount) {
              Instantiate(prefab, new Vector3 (Random.Range (-10000,100000), Random.Range (-10000,100000),Random.Range (-100000,100000)), Quaternion.identity);
              i++;
          }
      }

 
Comment
Add comment · Show 2 · 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
avatar image Owen-Reynolds · Dec 06, 2015 at 06:15 PM 0
Share

?? UniCreate never uses rnd. Yours works the same as original version.

Also, please check dates -- this is over a year old. It was brought up by ziggl's comment. There are tabs to select New questions, or Active questions. You have to be careful when you're in the Active tab.

avatar image Runalotski Owen-Reynolds · Dec 07, 2015 at 11:35 AM 0
Share

I didn't check how old it was. and yes uniCreate does not use rnd, i did not know the specific location it was needed so left that for the user to implement, it was to show to to pass a value to an rpc, which i felt answers the question.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning In Photon C# 1 Answer

Photon/Network - Killing A Client Enemy Unit 3 Answers

Changing the parent of a transform via RPC 0 Answers

Synchronize array with simultaneous access via Photon Network RPC 1 Answer

PhotonView with ID has no method marked with the [PunRPC](C#) property! BUT IT HAS ??? 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