PUNRPC call destroying the gameobject, even with nothing in the RPC function

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. 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!

Best Answers

  • Tobias
    Tobias admin
    Answer ✓

    How much data are you sending with the texture? If it's a lot (500kB+) you might get disconnected for sending it. Enable the SupportLogger and check the console log for anything suspicious.

    The way you log GetType().Name in OnDestroy, it will just show which class/component is logging this. Not who is calling the Destroy.

  • Nrike458
    Nrike458
    Answer ✓

    So, the file that I was sending was around 2MB in size, as it was an 8k texture. However, I could never find anything on why it was getting destroyed, no debug messages, just nothing. It's good to know about the SupportLogger for if these issues continue. When I reduced my texture down to around 100-300kB, things were working just fine.

Answers

  • Tobias
    Tobias admin
    Answer ✓

    How much data are you sending with the texture? If it's a lot (500kB+) you might get disconnected for sending it. Enable the SupportLogger and check the console log for anything suspicious.

    The way you log GetType().Name in OnDestroy, it will just show which class/component is logging this. Not who is calling the Destroy.

  • Nrike458
    Nrike458
    Answer ✓

    So, the file that I was sending was around 2MB in size, as it was an 8k texture. However, I could never find anything on why it was getting destroyed, no debug messages, just nothing. It's good to know about the SupportLogger for if these issues continue. When I reduced my texture down to around 100-300kB, things were working just fine.

  • Thanks for the update.

    Yes, sending > 500kB leads to a disconnect.