Home > database >  how to spawn unity player with filled game object fields in unity 2d
how to spawn unity player with filled game object fields in unity 2d

Time:09-11

The game is working correctly and there arent any other issues apart from the fact that the public fields from the players scripts that are supposed to be filled with game objects from the scene arent filled and im not sure how to do that.

heres an example from one of the scripts: image

and heres what it should look like: image

the joystick area from the second image is from the scene, not an asset: image

here is the code im using:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovePlayer : MonoBehaviour
{

public MovementJoystick movementJoystick;
public int playerSpeed;
private Rigidbody2D rb;
bool facingRight = true;
public Animator animator;
public float interval;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();
    playerSpeed = 7;
    interval = 10;
}

// Update is called once per frame
void FixedUpdate()
{
    if (movementJoystick.joystickVec.y != 0)
    {
        rb.velocity = new Vector2(movementJoystick.joystickVec.x * playerSpeed, movementJoystick.joystickVec.y * playerSpeed);
        animator.SetFloat("speed", Mathf.Abs(movementJoystick.joystickVec.x));
    }
    else
    {
        rb.velocity = Vector2.zero;
        animator.SetFloat("speed", Mathf.Abs(0));
    }

    if (movementJoystick.joystickVec.x < 0 && !facingRight)
    {
        Flip();
    }
    if (movementJoystick.joystickVec.x > 0 && facingRight)
    {
        Flip();
    }
}

void Update()
{
    if (playerSpeed == 14 && interval > 0)
    {
        interval -= Time.deltaTime;
    }

    else
    {
        playerSpeed = 7;
        interval = 10;
    }
}

void Flip()
{
    transform.Rotate(0f, 180f, 0f);

    facingRight = !facingRight;
}

public void SpeedControl(int newplayerSpeed)
{
    playerSpeed = newplayerSpeed;
}
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MovementJoystick : MonoBehaviour
{

public GameObject joystick;
public GameObject joystickBG;
public Vector2 joystickVec;
private Vector2 joystickTouchPos;
private Vector2 joystickOriginalPos;
private float joystickRadius;

// Start is called before the first frame update
void Start()
{
    joystickOriginalPos = joystickBG.transform.position;
    joystickRadius = joystickBG.GetComponent<RectTransform>().sizeDelta.y / 2;
}

public void PointerDown()
{
    joystick.transform.position = Input.mousePosition;
    joystickBG.transform.position = Input.mousePosition;
    joystickTouchPos = Input.mousePosition;
}

public void Drag(BaseEventData baseEventData)
{
    PointerEventData pointerEventData = baseEventData as PointerEventData;
    Vector2 dragPos = pointerEventData.position;
    joystickVec = (dragPos - joystickTouchPos).normalized;

    float joystickDist = Vector2.Distance(dragPos, joystickTouchPos);

    if (joystickDist < joystickRadius)
    {
        joystick.transform.position = joystickTouchPos   joystickVec * joystickDist;
    }

    else
    {
        joystick.transform.position = joystickTouchPos   joystickVec * joystickRadius;
    }
}

public void PointerUp()
{
    joystickVec = Vector2.zero;
    joystick.transform.position = joystickOriginalPos;
    joystickBG.transform.position = joystickOriginalPos;
}
}

this is how to instantiate the player using photon servers (what i am using)

    public GameObject playerToSpawn;
        PhotonNetwork.Instantiate(playerToSpawn.name, spawnPoint.position, Quaternion.identity);

CodePudding user response:

Create (or use an already existing) "Controller" script to do so, createa variable to save the reference to that object in "Controller" and once you instantiate prefab, use GetComponent to access the script attached to that object, then set reference to the attached script from controller.

public class MainController : MonoBehaviour
{
    // drag and drop that object here to save reference
    [SerializeField] private MovementJoystick movementJoystick;
    [SerializeField] private GameObject movePlayerPrefab;

    void Start()
    {
        var movePlayerObject = Instantiate(movePlayerPrefab);
        var movePlayerScript = movePlayerObject.GetComponent<MovePlayer>();
        // once you have reference to that newly instantiated object set saved reference with method
        movePlayerScript.SetJoystick(movementJoystick);
    }
}
public class MovePlayer : Monobehaviour
{
    private MovementJoystick movementJoystick;

    public void SetJoystick(MovementJoystick movementJoystick)
    {
        this.movementJoystick = movementJoystick;
    }
}
  • Related