0
\$\begingroup\$

I have big problem in my code .. I want to spawn obstacle exactly on the same position as on the server is spawned so I made rpc Method and tried to pass random variable of float , but when i run the game obstacles are spawned on the diffrent position. I am loosing a lot of time to get it done but I still can not find solution.

Here is the code

   [PunRPC]
    void RPC_SpawnColumn(Vector3 position)
    {
        // Instantiate(prefab, position, Quaternion.identity);
        Debug.Log("THIS IS SPAWNYPOSITION" + spawnYPosition);

        Instantiate(prefab, new Vector2(9, spawnYPosition), Quaternion.identity);

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        //print("Are we here");
        if (collision.GetComponent<ColumnsMultiplayer>() != null)
        {
            
            print("Or are we here");
            if (SpawnOnce)
            {
                spawnYPosition = Random.Range(-3.36f, 2.98f);
                
               
               
                photonView.RPC("RPC_SpawnColumn", RpcTarget.Others,new Vector3(9, spawnYPosition));  // this is 
              
            }
                //SpawnColumn();
                
            SpawnOnce = false;


        }
    }

Thank you so much for reading post.

Edit: I need to send spawnYPosition from method two to method one and spawn it with same position of spawnYPosition in the second method.

\$\endgroup\$
2
  • \$\begingroup\$ It looks like you accept a position argument in the RPC call, but none of your code in that method actually reads or uses that position variable. Is this intentional? Have you tried actually reading & using the value that was passed from the caller? \$\endgroup\$
    – DMGregory
    Mar 28, 2019 at 13:50
  • \$\begingroup\$ @DMGregory , Can you tell me how i can use it? or how i can read the spawnYPosition = Random.Range(-3.36f, 2.98f); in the first method, i mean how i can read spawnYPosition and pass it into rpc spawn method on the variable of the "spawnYPosition" \$\endgroup\$ Mar 28, 2019 at 13:53

1 Answer 1

0
\$\begingroup\$

It looks like you might need a refresher on function arguments.

When you define a function like this:

void RPC_SpawnColumn(Vector3 position)

You're saying "I will accept as input a Vector3 value, and I will call that value position inside the body of this method" - this position variable is called an argument to the function, and you can use it like other variables inside its body.

Now, when you call the function (remotely) like this:

photonView.RPC("RPC_SpawnColumn", RpcTarget.Others, new Vector3(9, spawnYPosition)); 

You're saying "Call the function, and pass it the value (9, spawnYPosition, 0) to use as the position argument"

So, inside the body of RPC_SpawnColumn, if you want the value of spawnYPosition which you passed as the y coordinate of the position argument, you can get it by reading position.y.

So it looks like you want to change this line, which makes up a new vector with whatever value we have in spawnYPosition locally:

Instantiate(prefab, new Vector2(9, spawnYPosition), Quaternion.identity);

to this line, which uses the position that was sent to us via the RPC call (including spawnYPosition in the y component):

Instantiate(prefab, position, Quaternion.identity);
\$\endgroup\$
5
  • \$\begingroup\$ I marked it as answer so i need to make it something like hits in rpc method:: void RPC_SpawnColumn(Vector3 position) { Instantiate(prefab,position, Quaternion.identity); } \$\endgroup\$ Mar 28, 2019 at 14:15
  • \$\begingroup\$ When I use your solution it spawn objects on the diffrent positions .. It doesn't work in my case.. I hope you will send me email here : [email protected] ..... I dont have any other idea why it doesn't work... here is full code of obstacle spawn with your edit. LINK to fullcode now: pastebin.com/mHPf7hkA \$\endgroup\$ Mar 28, 2019 at 14:30
  • \$\begingroup\$ Users here will not troubleshoot your problem via email. It is your responsibility to ensure that your question contains a minimal, complete, verifiable example of the problem so that users can validate solutions without access to all of your code or back-and-forth discussion. \$\endgroup\$
    – DMGregory
    Mar 28, 2019 at 14:40
  • \$\begingroup\$ One guy said to me i need to use instantiate in photonnetwork not rpc \$\endgroup\$ Mar 28, 2019 at 15:08
  • \$\begingroup\$ Your question was "how to send float" which is answered here. If you want to do network instantiation, first read the docs about how to do that and work through some existing guides & tutorials to get the hang of it. If you run into trouble, you can post a new question about the specific issue you've run into. \$\endgroup\$
    – DMGregory
    Mar 28, 2019 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .