at first I am not a native English speaker, so excuse my common mistakes.
Right now I am developing a simple multiplayer game for my studies in Unity (esp. with Photon).
In the first scene of my game the players spawn an can run around the environment. If player 1 hits a trigger then there ist a transition to the second level.
In the scond level there is something like a pinboard (used for a creative brainstorming), because it is an educational game. So I created a central Canvas/Panel as a background in the scene. When the players switch to this scene, every player gets their own input field (as child of the player prefab). If the master client writes something into the input field and presses Enter, then the script instantiates a card (image) with the same text on it. The card becomes a child of the central Panel in the scene (solved with a RPC message).
Now here is my problem: The non master client can see the new card, but the text is totally random or not appearing. It seems like it has no access to the correct text. In addition to that, the non master client is not allowed to instantiate a card to the central panel in the scene. Im am sure there is an easy way to fix this, but I dont really now what kind of solution I am looking for?
Or is there any better approach to reach my goal? At first I tried to use only one input field in the scene but I thought this is a bad idea, so I gave all players their own one. With this approach I reached the exact result as shown above.
I just started trial and error again. I give you an example for my code:
public void CreatePostIt()
{
if (color == "Yellow")
{
int randomX = Random.Range(-344, 344);
int randomY = Random.Range(-170, 170);
float randomRotation = Random.Range(-12.0f, 12.0f);
helpText = iField.text.ToString();
GameObject childObject = PhotonNetwork.InstantiateSceneObject("YellowPostIt_Prefab", new Vector3(randomX, randomY, 0), Quaternion.Euler(0, 0, randomRotation), 0, null) as GameObject;
childObject.GetComponentInChildren<Text> ().text = helpText.ToString ();
MyPhotonView = childObject.GetComponent<PhotonView>();
this.MyPhotonView.RPC ("GitParent", PhotonTargets.AllBuffered);
}
As you can see I instantiate a new Gameobject, then I set the text and call my RPC Message. In the RPC method I only set the parent so the instantiated Gameobject becomes child of the Panel in my Scene:
All players can see the new Gameobject in the correct position and as a child of the panel (checked it in the hierarchy window). But the correct text is only shown in the masterâs client. The text in the non master client seems to be struggling. I think there are interferences by using different input field, so the non master clients want to use their own text for instantiation. I have no idea to fix thisâŚ
You need to set the text by passing the text string to an RPC, you could modify your current GitParent RPC to accept a string argument, and set the text there.
Or you could pass the text string in the instantiationdata, and set the text on the child object in the OnPhotonInstantiate callback.
I got another question about drag and drop! I use a simple script like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
void Start()
{
}
void Update()
{
}
public void OnBeginDrag(PointerEventData eventData)
{
}
public void OnDrag(PointerEventData eventData)
{
this.transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
}
}
In my main level everything works just fine. I can pull a prefab of my UI element (with the script on it) into a canvas and dragging works instantly.
In my second level is an other canvas, it does not work in this level. What am I missing? Anything special needed? I tried the script in a empty project and it also worked, so I wonder what could be the bottle neck in my second level!?
No mouse input accessible?
Edit: I know the answer: My player prefab has its own canvas which overlays the background canvas. Hope I can fix this! ---- Ok it was not ne canvas, it was the panel! So I scaled the playerâs panel (for input field) to the minimum. Now it works just fine!
I can successfully instantiate my network object. I have a script with an ownership transfer for drag and drop functions.
Now my cards have a button (as child object) to destroy them:
public void DestroyOnClick()
{
this.GetComponentInParent<PhotonView> ().RequestOwnership ();
PhotonNetwork.Destroy(transform.parent.gameObject);
}
But the non ownership clients can not push the button to destroy the card. When I drag the cards then the ownership transfer works and the (formerly) non ownership client can push the button from now on.
I wonder if the time between ownership request and the destroy function are too close? Any ideas?
Is it triggered by any ownership transfer or can I manipulated this, so that only a specific ownership request starts this callback? In my case there are different cases in which I need to transfer ownership!
âThisâ should be the button the script is put on, then I wanted to get the ownership from its parent. The ownership transfer did not work in this case. So I created a public GameObject variable (parentObject) and dragged the parent on the right place in the inspector and changed my code to:
Itâs triggered by any ownership request, but itâs up to you which objects subscribe to the event. Also the callback parameter contains the viewID of the transferred object and the new and old owner, so you should have all the information you need to ensure you only handle it on the object that you want to.
Iâm not sure at first glance why this approach works. As far as I can see if you are trying to destroy the object on the line of code immediately following the ownership transfer request, there can be no guarantee that the transfer has happened when the Destroy is called.
Many thanks for your help Munchy. I solved all my issues in this part of my game. I guess closing this thread and creating a new one for further questions is the way to go.