2

The gameobjects in the array (SequenceItems) have at least 1 child, and in some cases 1 child and 1 grandchild. the Mesh Swapping works but it is very jittery when there are two players in the room. Also, when I place a sphere inside the mesh that collides with another mesh, the for loop (resizeCube) seems to stop But the OnNext() continues.

Edit** I changed my code to swap meshes instead of adjusting the scale but I am still having the same issues. Here is my code:

    using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using Microsoft.MixedReality.Toolkit.UI;
using TMPro;


public class PCSequencer : MonoBehaviour
{
    public bool play = true;
    [Header("Sequence Controller")]
    private PhotonView myPV;
    public Mesh on;
    public Mesh off;
    [Tooltip("Drag in a bunch of GameObject you want in the sequence (must be in correct order before dragging in).")]
    /// <summary>
    /// List of GameObjects in sequence (must be in correct order)
    /// </summary>
    public GameObject[] SequenceItems;
    [Tooltip("The amount of time in seconds between sequence items.")]
    /// <summary>
    /// The interval of time in seconds between sequences.
    /// </summary>
    public float sequenceIntervalDelay = 2f;
    private int _currentIndex;
    [SerializeField]
    private TextMeshPro textMesh = null;
    /// <summary>
    /// The current sequence index.
    /// </summary>
    public int CurrentIndex


    {
        get { return _currentIndex; }
        set
        {
            if (value != _currentIndex)
            {
                // Ensure current index isn't more/less than the index bounds of the given Sequence Items...
                if (_currentIndex >= 0 || _currentIndex <= SequenceItems.Length - 1)
                {
                    // Debug.Log("is this a thing?:   " + value);
                    _currentIndex = value;
                    textMesh.text = _currentIndex.ToString();
                }
            }
        }
    }
    /// <summary>
    /// Get things ready before first update occurs.
    /// </summary>
    void Start()
    {
        myPV = GetComponent<PhotonView>();
        // Ensure Sequence is reset and only the first Sequence Item is visible...
        ResetSequence();

        OnPlay();


    }
    public void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            OnPlay();
            print("space key was pressed");
        }
    }

    /// <summary>
    /// On play button pressed.
    /// </summary>
    private void OnPlay()
    {
        // Reset sequence back to start...
        if (CurrentIndex == SequenceItems.Length - 1)
        {
            ResetSequence();
        }

        // When Play button press, repeat call the OnNext() method...
        StartCoroutine("func");
        // InvokeRepeating("OnNext", sequenceIntervalDelay, sequenceIntervalDelay);
        Debug.Log("Sequence Item " + CurrentIndex);
    }


    IEnumerator func()
    {
        while (true)
        {
            OnNext();

            yield return new WaitForSecondsRealtime(sequenceIntervalDelay); //Wait 1 second

        }
    }




    private void OnNext()
    {
        if (play == true)
        {
            if (CurrentIndex < SequenceItems.Length)
            {
                // Debug.Log("what the index:  " + CurrentIndex);

                if (PhotonNetwork.IsMasterClient)
                {
                    myPV.RPC("ResizeCube", RpcTarget.All, CurrentIndex);
                }
                //ResizeCube(CurrentIndex);

                if (PhotonView.Find(SequenceItems[CurrentIndex].GetComponent<PhotonView>().ViewID).gameObject.GetComponent<pcInteraction>().isActiveToPlay == true)
                {
                    Debug.Log("PlayNOte:  " + CurrentIndex);
                    SerialCommunication.sendNote(PhotonView.Find(SequenceItems[CurrentIndex].GetComponent<PhotonView>().ViewID).GetComponent<pcInteraction>().allCheckInsideSphere.currentNote);
                    Debug.Log("sending note to arduino from sequencer");

                }
                else if (SequenceItems[CurrentIndex].GetComponent<pcInteraction>().isActiveToPlay == false)
                {

                }
                CurrentIndex++;
            }
            else
            {


                ResetSequence();


            }
        }
    }

    public void OnSliderUpdated(SliderEventData eventData)
    {
        float current = float.Parse($"{eventData.NewValue:F2}");
        myPV.RPC("SpeedAdjust", RpcTarget.All, current);

    }

    [PunRPC]
    void SpeedAdjust(float speed)
    {
        sequenceIntervalDelay = speed;

    }

    [PunRPC]
    void ResizeCube(int index)
    {

        {
            for (int i = 0; i < SequenceItems.Length; i++)
            {
                if (i == index)
                    SequenceItems[i].GetComponent<MeshFilter>().mesh = on;
                // PhotonView.Find(SequenceItems[i].GetComponent<PhotonView>().ViewID).gameObject.GetComponent<MeshFilter>().mesh = on;
                //  SequenceItems[i].gameObject.GetComponent<MeshFilter>().mesh = on;

                else
                    SequenceItems[i].GetComponent<MeshFilter>().mesh = off;
                //PhotonView.Find(SequenceItems[i].GetComponent<PhotonView>().ViewID).gameObject.GetComponent<MeshFilter>().mesh = off;
                // SequenceItems[i].gameObject.GetComponent<MeshFilter>().mesh = off;
                //PhotonView.Find(SequenceItems[i].GetComponent<PhotonView>().ViewID).gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
            }
        }
    }

    /// <summary>
    /// Resests the sequence back to the beginning.
    /// </summary>
    private void ResetSequence()
    {
        foreach (var go in SequenceItems)
        {
            // go.SetActive(false);
            //  go.GetComponent<MeshRenderer>().material.color = Color.white;
        }
        // SequenceItems.First().SetActive(true);
        CurrentIndex = 0;
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
cyo
  • 69
  • 1
  • 14
  • why do you use `PhotonView.Find` if you already have a reference to the according `PhotonView`/`GameObject` anyway? Simply do `SequenceItems[i].transform.localScale = ...` and then how many items are there in `SequenceItems` and how often do you call `ResizeCube` ? – derHugo Sep 12 '22 at 08:23
  • The reason I’m using Find is I’m trying to address the items in the array for each player in each room. is it possible that if I address the photonView alone that will target all the items for each player in the room? There are 6 items in the array and I call ResizeCube in the update function – cyo Sep 12 '22 at 15:20
  • `SequenceItems[i]` already seems to be a `GameObject` with a `PhotonView` attached. You are using the `ViewID` and pass it into `PhotonView.Find` .. which will return the exact same `PhotonView` you already have anyway. That's what I mean primarily. And then further an issue might be that those other `PhotonView` are **not** `isMine` and therefore you an not simply scale them as they are overruled with whatever you receive from the actual owner of those views! You might rather need to again pass on a command to scale them on the owner side and sync it back to everyone else (including you) – derHugo Sep 13 '22 at 07:09
  • Bumping this because I am still having issues... – cyo Sep 15 '22 at 20:09
  • I don't know about the jitter, but keep in mind that your coroutine can be called multiple times (you are not storing a reference or something to control that), I don't know if this is intended, or if it's the cause of the jitter, do not looks like that, but just to keep in mind ^^ – Lotan Sep 22 '22 at 10:33

0 Answers0