EventTrigger on button not recognizing instantsiated player
So I'm working on a multiplayer game for mobile devices and I have these buttons with event triggers on them so when I click them they move the player forward and so on. Now I have it so OnPress the buttons turn a boolean true and when it is true the player ship moves according to which buttons is pressed. But since the Canvas and buttons that make the player move have no scripts attached, the event trigger has to get the inputs from the player. Now the problem is that the event triggers needed a gameobject to know what to do for when each button is pressed. So i dragged the prefab player spaceship into there and set it up so it should work. The only problem is when the player gets instantsiated over the network, the buttons no longer work since it is trying to access the prefab that has everything deactivated, unless it is your photon view. (I am using Photon Networking) So if i manually turn the booleans true the ship does move, but the buttons don't work.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class PlayerMovementTouch : MonoBehaviour, IEventSystemHandler
{
public float Speed = 15;
public float RotSpeed = 180;
private Rigidbody2D playerRigidbody;
private TouchMovement touchMovement;
public bool buttonHeldUp = false;
public bool buttonHeldDown = false;
void Start()
{
if (!this.gameObject.GetComponent<PlayerNetworkMover> ().IsMyView)
{
GameObject.Find("CanvasButton").SetActive(false);
}
playerRigidbody = GetComponent<Rigidbody2D>();
}
public void pressedUp (BaseEventData eventData)
{
buttonHeldUp = true;
}
public void notpressedUp (BaseEventData eventData)
{
buttonHeldUp = false;
}
public void pressedDown (BaseEventData eventData)
{
buttonHeldDown = true;
}
public void notpressedDown(BaseEventData eventData)
{
buttonHeldDown = false;
}
public void Update()
{
if (buttonHeldUp == true)
{
// Do stuff
Debug.Log ("clickUp");
// Move Character
playerRigidbody.AddForce(transform.up * Speed);
}
else
Debug.Log ("not click Up");
if (buttonHeldDown == true)
{
// Do stuff
Debug.Log ("clickDown");
// Move Character
playerRigidbody.AddForce(transform.up * -Speed);
}
else
Debug.Log ("not click Down");
}
}
Any help is would be great!
Answer by dhotlo2 · Nov 02, 2016 at 09:02 PM
I have had this same exact issue for 2 days now, have you found a solution to it?