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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class SauceScriptTomato : NetworkBehaviour
{
public static SauceScriptTomato inst;
public Quaternion Pouringrot, Originalrot;
[SerializeField] private LayerMask PickMasksauce;
[SerializeField] private Color SauceTomatoColor, SauceHotColor, SusColor;
public GameObject SaucePart;
public Rigidbody rigid;
public bool isSaucing;
public float SauceT, PourT;
public Transform raypos;
public float SauceRange;
public float rotspeed, saucespeed;
public bool CanRot;
public bool IsRot;
void Awake()
{
inst = this;
}
// Start is called before the first frame update
void Start()
{
rigid = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetMouseButtonDown(1) && CanRot)
{
IsRot = true;
}
else if (Input.GetMouseButtonUp(1) && CanRot)
{
IsRot = false;
}
if (IsRot)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Pouringrot, Time.deltaTime * rotspeed);
Ray raysauce = new Ray(raypos.position, raypos.up);
GameObject PizzaDough;
Debug.DrawRay(raysauce.origin, raysauce.direction * SauceRange, Color.red);
PourT += Time.deltaTime;
if (PourT >= 0.25f)
{
PourT = 0.25f;
SaucePart.SetActive(true);
}
if (Physics.Raycast(raysauce, out RaycastHit HitInfoSauce, SauceRange, PickMasksauce) && HitInfoSauce.transform.tag == "PizzaLay")
{
PizzaDough = HitInfoSauce.transform.gameObject;
isSaucing = true;
}
else
{
isSaucing = false;
PizzaDough = null;
}
if (isSaucing && PizzaDough.GetComponent<PizzaLayScript>().IsBurned == false)
{
saucespeed += Time.deltaTime;
MeshRenderer meshrendpizza;
meshrendpizza = HitInfoSauce.transform.gameObject.GetComponent<MeshRenderer>();
if (gameObject.tag == "TomatoSauce")
{
meshrendpizza.materials[1].color = Color.Lerp(meshrendpizza.materials[1].color, SauceTomatoColor, SauceT);
if (saucespeed >= 0.10f)
{
HitInfoSauce.transform.GetComponent<PizzaLayScript>().TomatoSauceAmount++;
HitInfoSauce.transform.GetComponent<PizzaLayScript>().HotSauceAmount -= 1;
HitInfoSauce.transform.GetComponent<PizzaLayScript>().SusSauceAmount -= 1;
saucespeed = 0f;
}
}
else if (gameObject.tag == "HotSauce")
{
meshrendpizza.materials[1].color = Color.Lerp(meshrendpizza.materials[1].color, SauceHotColor, SauceT);
if (saucespeed >= 0.10f)
{
HitInfoSauce.transform.GetComponent<PizzaLayScript>().HotSauceAmount++;
HitInfoSauce.transform.GetComponent<PizzaLayScript>().TomatoSauceAmount -= 1;
HitInfoSauce.transform.GetComponent<PizzaLayScript>().SusSauceAmount -= 1;
saucespeed = 0f;
}
}
else if (gameObject.tag == "SusSauce")
{
meshrendpizza.materials[1].color = Color.Lerp(meshrendpizza.materials[1].color, SusColor, SauceT);
if (saucespeed >= 0.10f)
{
HitInfoSauce.transform.GetComponent<PizzaLayScript>().SusSauceAmount++;
HitInfoSauce.transform.GetComponent<PizzaLayScript>().HotSauceAmount -= 1;
HitInfoSauce.transform.GetComponent<PizzaLayScript>().TomatoSauceAmount -= 1;
saucespeed = 0f;
}
}
}
}
else
{
transform.rotation = Quaternion.Lerp(transform.rotation, Originalrot, Time.deltaTime * rotspeed);
PourT = 0f;
SaucePart.SetActive(false);
}
if (rigid.useGravity == true)
{
CanRot = false;
transform.rotation = Originalrot;
PourT = 0f;
SaucePart.SetActive(false);
IsRot = false;
}
}
[ServerRpc(RequireOwnership = false)]
public void GiveOwnershipServerRpc(ulong clientId)
{
GetComponent<NetworkObject>().ChangeOwnership(clientId);
}
Any clue on what’s going on? Everything is welcome!