Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question ¿How can i change a material during runtime in netcode? (owner authoritive)

Discussion in 'Netcode for GameObjects' started by AngelHYYPE09, Dec 1, 2023.

  1. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    Sorry if my english is not too god haha
    So i've been making a Multiplayer game using Netcode for GameObjects, and i want to change the material of a object in runtime, but when i try to do that, the color doesn't synchronize, which means that when the host changes the color, only the host can see the color, and when the client changes the color, only the client can see the color.
    It's important to mention that i change the color with a RAYCAST, and the object that shoots the raycast also has some particles that activate when the raycast is launched, but it isn't synchronized as well.
    Just in case, here's the code for the object that changes the color of the external object:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class SauceScriptTomato : NetworkBehaviour
    7. {
    8.     public static SauceScriptTomato inst;
    9.     public Quaternion Pouringrot, Originalrot;
    10.     [SerializeField] private LayerMask PickMasksauce;
    11.     [SerializeField] private Color SauceTomatoColor, SauceHotColor, SusColor;
    12.     public GameObject SaucePart;
    13.     public Rigidbody rigid;
    14.     public bool isSaucing;
    15.     public float SauceT, PourT;
    16.     public Transform raypos;
    17.     public float SauceRange;
    18.     public float rotspeed, saucespeed;
    19.     public bool CanRot;
    20.     public bool IsRot;
    21.  
    22.     void Awake()
    23.     {
    24.         inst = this;
    25.     }
    26.  
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.         rigid = GetComponent<Rigidbody>();
    31.     }
    32.    
    33.     void Update()
    34.     {
    35.         if (Input.GetMouseButtonDown(1) && CanRot)
    36.         {
    37.             IsRot = true;
    38.         }
    39.         else if (Input.GetMouseButtonUp(1) && CanRot)
    40.         {
    41.             IsRot = false;
    42.         }
    43.  
    44.         if (IsRot)
    45.         {
    46.             transform.rotation = Quaternion.Lerp(transform.rotation, Pouringrot, Time.deltaTime * rotspeed);
    47.             Ray raysauce = new Ray(raypos.position, raypos.up);
    48.             GameObject PizzaDough;
    49.             Debug.DrawRay(raysauce.origin, raysauce.direction * SauceRange, Color.red);
    50.             PourT += Time.deltaTime;
    51.  
    52.             if (PourT >= 0.25f)
    53.             {
    54.                 PourT = 0.25f;
    55.                 SaucePart.SetActive(true);
    56.             }
    57.  
    58.             if (Physics.Raycast(raysauce, out RaycastHit HitInfoSauce, SauceRange, PickMasksauce) && HitInfoSauce.transform.tag == "PizzaLay")
    59.             {
    60.                 PizzaDough = HitInfoSauce.transform.gameObject;
    61.                 isSaucing = true;
    62.             }
    63.             else
    64.             {
    65.                 isSaucing = false;
    66.                 PizzaDough = null;
    67.             }
    68.  
    69.             if (isSaucing && PizzaDough.GetComponent<PizzaLayScript>().IsBurned == false)
    70.             {
    71.                 saucespeed += Time.deltaTime;
    72.                 MeshRenderer meshrendpizza;
    73.                 meshrendpizza = HitInfoSauce.transform.gameObject.GetComponent<MeshRenderer>();
    74.                 if (gameObject.tag == "TomatoSauce")
    75.                 {
    76.                     meshrendpizza.materials[1].color = Color.Lerp(meshrendpizza.materials[1].color, SauceTomatoColor, SauceT);
    77.                     if (saucespeed >= 0.10f)
    78.                     {
    79.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().TomatoSauceAmount++;
    80.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().HotSauceAmount -= 1;
    81.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().SusSauceAmount -= 1;
    82.                         saucespeed = 0f;
    83.                     }
    84.                 }
    85.                 else if (gameObject.tag == "HotSauce")
    86.                 {
    87.                     meshrendpizza.materials[1].color = Color.Lerp(meshrendpizza.materials[1].color, SauceHotColor, SauceT);
    88.                     if (saucespeed >= 0.10f)
    89.                     {
    90.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().HotSauceAmount++;
    91.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().TomatoSauceAmount -= 1;
    92.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().SusSauceAmount -= 1;
    93.                         saucespeed = 0f;
    94.                     }
    95.                 }
    96.                 else if (gameObject.tag == "SusSauce")
    97.                 {
    98.                     meshrendpizza.materials[1].color = Color.Lerp(meshrendpizza.materials[1].color, SusColor, SauceT);
    99.                     if (saucespeed >= 0.10f)
    100.                     {
    101.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().SusSauceAmount++;
    102.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().HotSauceAmount -= 1;
    103.                         HitInfoSauce.transform.GetComponent<PizzaLayScript>().TomatoSauceAmount -= 1;
    104.                         saucespeed = 0f;
    105.                     }
    106.                 }
    107.                
    108.             }
    109.         }
    110.         else
    111.         {
    112.             transform.rotation = Quaternion.Lerp(transform.rotation, Originalrot, Time.deltaTime * rotspeed);
    113.             PourT = 0f;
    114.             SaucePart.SetActive(false);
    115.         }
    116.  
    117.         if (rigid.useGravity == true)
    118.         {
    119.             CanRot = false;
    120.             transform.rotation = Originalrot;
    121.             PourT = 0f;
    122.             SaucePart.SetActive(false);
    123.             IsRot = false;
    124.         }
    125.     }
    126.  
    127.  
    128.     [ServerRpc(RequireOwnership = false)]
    129.     public void GiveOwnershipServerRpc(ulong clientId)
    130.     {
    131.         GetComponent<NetworkObject>().ChangeOwnership(clientId);
    132.     }
    133.  
    134.  
    Any clue on what's going on? Everything is welcome!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,058
    I don't see where you are synchronizing this change. There is one ServerRpc and that's for changing ownership, but I don't see where that's being called. The rest of the code doesn't concern itself at all with synchronizing network state.

    Btw DRY principle! Instead of repeating this over and over again:
    HitInfoSauce.transform.GetComponent<PizzaLayScript>()

    Put it in a variable and use that. More readable code, and faster:
    var pizzaLay = HitInfoSauce.transform.GetComponent<PizzaLayScript>();
     
  3. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    Hi there! Thanks for your response! So the ServerRpc for the ownership gets called on the player script, so it doesn't show up there, but it does work.
    The problem is that the material change doesn't get synchronized, i tried to make another ServerRpc for changing the color and it did work on the host, since he can see the changes that the client does, but the client can't see the changes that the host does. Any suggestion?

    btw i ended up using your suggestion, now my code looks nice hahah
     
  4. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    I forgot to mention that there are some variables that this script changes for the other script called "PizzaLayScript", but they don't change if the client tries to, however the host can do that.