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\$
  • \$\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 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\$ – Simple aPPS Mar 28 at 13:53
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\$
  • \$\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\$ – Simple aPPS Mar 28 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 : cikatuna@gmail.com ..... 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\$ – Simple aPPS Mar 28 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 at 14:40
  • \$\begingroup\$ One guy said to me i need to use instantiate in photonnetwork not rpc \$\endgroup\$ – Simple aPPS Mar 28 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 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.