Search Unity

Third Party PUN2 RPC call destroying the gameobject, even with nothing in the RPC function

Discussion in 'Multiplayer' started by Nrike458, Apr 22, 2023.

  1. Nrike458

    Nrike458

    Joined:
    Oct 6, 2019
    Posts:
    37
    Hello,

    I've been trying to figure out this issue now for the past couple days, with no luck in getting things fixed. I'm basically trying to take a texture file from another Unity plugin, Paint in 3D, convert it to a byte array, send it across the network, and then convert it back to an image texture to apply to the players personal car. However, I'm having some issues with the code below where the object this script is on gets destroyed pretty much immediately.


    using PaintIn3D;

    using Photon.Pun;

    using Photon.Realtime;

    using System;

    using System.Collections;

    using System.Collections.Generic;

    using Unity.VisualScripting;

    using UnityEngine;


    public class LoadMultiplayerPaint : MonoBehaviourPunCallbacks

    {

    //private static byte[] carTexture;


    public override void OnEnable()

    {

    base.OnEnable();

    }


    public override void OnDisable()

    {

    base.OnDisable();

    }


    public void SetCarTexture()

    {

    string saveName = PlayerPrefs.GetString("PaintName");

    byte[] carTexture = P3dCommon.LoadBytes(saveName);

    PhotonView photonView = this.GetComponent<PhotonView>();

    photonView.RPC("RPC_SetCarTexture", RpcTarget.All, carTexture);

    }


    public static Texture2D LoadCarTexture(byte[] carTexture)

    {

    Texture2D tex = new Texture2D(2, 2);

    ImageConversion.LoadImage(tex, carTexture);

    return tex;

    }


    [PunRPC]

    private void RPC_SetCarTexture(byte[] carTexture)

    {

    Debug.Log("RPC Call");

    try

    {

    Debug.Log("Setting Car Texture");

    Renderer renderer = this.GetComponent<Renderer>();

    Debug.Log("Car Texture Set");



    if (renderer != null && renderer.material != null)

    {

    renderer.material.mainTexture = LoadCarTexture(carTexture);

    }

    else

    {

    Debug.LogError("Renderer or Material is null.");

    }



    }

    catch (Exception e)

    {

    Debug.LogError(e.Message);

    }


    }


    private void OnDestroy()

    {

    Debug.Log("Object is being destroyed from " + gameObject.name + " with script " + GetType().Name);

    }


    From the code above, when I call the RPC function, it does log the setting and set car textures, and doesn't throw any further messages in the try catch from there, but the object is deleted. When I comment out/remove all of the code in the RPC function, the object still gets deleted. However, when I don't call the RPC function at all, the object remains, but obviously it doesn't attempt to set the textures properly. I'm aware that the code to apply the texture probably doesn't work right, but I can't verify that until the object stops destroying itself. Anyways, I'm pretty sure it's something to do with the RPC call, but I'm not sure why that would be the case. This script is "started" by calling the "SetCarTexture" function by the separate object instantiating this prefab, and this script is on the prefab/instances of the prefab. I've tried searching many many many things on Google, and even tried Chat-GPT, but no luck in getting these issues to go away. Any help that could be provided on what I should try would be very helpful!
     
  2. Nrike458

    Nrike458

    Joined:
    Oct 6, 2019
    Posts:
    37
    So, I rewrote the script quite a bit, and figured out a better way to get the byte array of the texture from Paint in 3D, and to apply the byte array to texture to the object. However, I'm still having issues with the object being destroyed during/after the RPC call. I added debug statements to each step of the process, it logs the messages throughout the entirety of the script and RPC function, but is still destroyed. When I comment out the RPC call line, and not run the RPC function at all, the object remains, leading me to believe it is something to do with the RPC call. The debug statement of the length of the byte array is 2556230 bytes, or 2MB if I converted properly, so I'm not sure if that's the issue or not. The byte array is of a converted texture of 8192x8192 pixels in size.

    Code (CSharp):
    1. using ExitGames.Client.Photon;
    2. using PaintIn3D;
    3. using Photon.Pun;
    4. using Photon.Realtime;
    5. using System;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8. using Unity.VisualScripting;
    9. using UnityEngine;
    10.  
    11. public class LoadMultiplayerPaint : MonoBehaviourPunCallbacks
    12. {
    13.     public void Start()
    14.     {
    15.         SetCarTexture();
    16.     }
    17.  
    18.     public void SetCarTexture()
    19.     {
    20.         if (!photonView.IsMine)
    21.         {
    22.             return;
    23.         }
    24.  
    25.         string saveName = PlayerPrefs.GetString("PaintName");
    26.         Debug.Log(saveName);
    27.         P3dPaintableTexture carTexturePaint = this.GetComponent<P3dPaintableTexture>();
    28.         carTexturePaint.Load(saveName);
    29.         byte[] carTextureBytes = carTexturePaint.GetPngData();
    30.         Debug.Log(carTextureBytes.Length);
    31.  
    32.         photonView.RPC("RPC_CarTexture", RpcTarget.All, carTextureBytes);
    33.     }
    34.  
    35.     [PunRPC]
    36.     private void RPC_CarTexture(byte[] encodedCarTexture)
    37.     {
    38.         Debug.Log("1");
    39.         P3dPaintableTexture carTexturePaint = this.GetComponent<P3dPaintableTexture>();
    40.         Debug.Log("2");
    41.         carTexturePaint.LoadFromData(encodedCarTexture);
    42.         Debug.Log("3");
    43.     }
    44. }
     
  3. Nrike458

    Nrike458

    Joined:
    Oct 6, 2019
    Posts:
    37
    Ok, so I think I figured something out. I tried to modify the code to take that byte array from earlier, convert to a texture2d, resize it smaller, then convert back to a byte array to send over rpc. Scaling the texture down in half, the byte count went to just over 300,000 bytes, and the game object didn't get deleted. However, the texture isn't being applied properly from the rpc call.
     
  4. Nrike458

    Nrike458

    Joined:
    Oct 6, 2019
    Posts:
    37
    Alright, I figured out what I needed to do. I had some funky code going on in a rewrite of the code that was throwing issues with the texture not being applied properly. The initial with things being destroyed was due to the byte array being too large.
     
    tobiass likes this.
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Thanks for the update.