-1

For a very simple, minimalistic unity game I developed myself, I am trying to retrieve the positions for every player. I am very new to both C# and Unity.

I am following this tutorial: https://github.com/ethanedits/Universal-Unity-ESP (disclaimer I am not trying to develop this in any way that would make me gain an unfair advantage in any multiplayer game. This is strictly for personal education purposes).

I'm trying to identify the right name of the Player objects in order to get their positions with transform() in the hax.cs file which originally is like this:

namespace Universal_Unity_ESP
{
    class Hacks : MonoBehaviour
    {
        public void OnGUI()
        {
            foreach (Player player in FindObjectsOfType(typeof(Player)) as Player[])
            {
                //In-Game Position
                Vector3 pivotPos = player.transform.position; //Pivot point NOT at the feet, at the center

Problem is, when I made the game, I didn't declare a player class per se. I am using the Photon add-in, so my player object in Unity is within the Assets/resources folder.

photon.resource

For me, the player is just declared as an object contained in the RoomManager class, and I'm not sure how I should adapt the above code. This is what I tried, with the errors in comments.

using UnityEngine;
using Photon.Realtime;
namespace Universal_Unity_ESP
{
    class Hacks : MonoBehaviour
    {
        public void OnGUI()
        {
            foreach (Player player in FindObjectsOfType(typeof(Player)) as Player[]) //ERROR: cannot convert type unityengine.object to Photon.realtime.player
            {
                //In-Game Position
                Vector3 pivotPos = player.transform.position; //ERROR: player does not contain a definition for transform

My roomManager.cs script is below with two underlined parts. As you can see the GameObject player is an object within the roomManager and not a Class by itself. It seems to be instantiated through PhotonNetwork.Instantiate, which does not inherit from Monobehaviour, meaning I'm not able to retrieve its position in the above code (for which I also had to be using photon.realtime, which probably isn't the right approach).

There is also a Player.cs file in \Assets\Photon\PhotonRealtime\Code\Player.cs file) but it also does not inherit from Monobehaviour and I don't think that this is the right approach. Any help more than welcome.

My RoomManager.cs code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class RoomManager : MonoBehaviourPunCallbacks
{
    public static RoomManager instance;
    public GameObject player;
    [Space]
    public Transform[] spawnPoints;
    [Space]
    public GameObject roomCam;
    [Space]
    public GameObject nameUI;
    public GameObject connectingUI;
    private string nickname = "unnamed";

    void Awake() {
        instance = this;
    }
    public void ChangeNickname(string _name) {
        nickname = _name;
    }
    public void JoinRoomButtonPressed() {
        Debug.Log("Connecting...");
        PhotonNetwork.ConnectUsingSettings();

        nameUI.SetActive(false);
        connectingUI.SetActive(true);
    }
    // Start is called before the first frame update
    void Start()
    {

    }
public override void OnConnectedToMaster()
{
    base.OnConnectedToMaster();
    Debug.Log("Connected to Server");
    PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby() {
    base.OnJoinedLobby();
    PhotonNetwork.JoinOrCreateRoom("test", null, null);
    Debug.Log("Connected, in the Lobby");
}
public override void OnJoinedRoom() {
    base.OnJoinedRoom();
    Debug.Log("Connected, in a room");

    roomCam.SetActive(false);
    SpawnPlayer();
    }

public void SpawnPlayer() {
    Transform spawnPoint = spawnPoints[UnityEngine.Random.Range(0,spawnPoints.Length)];
    GameObject _player = PhotonNetwork.Instantiate(player.name, spawnPoint.position, Quaternion.identity);
    _player.GetComponent<PlayerSetup>().IsLocalPlayer();
    _player.GetComponent<Health>().isLocalPlayer = true; // setting the attribute to true in the health script
    
    _player.GetComponent<PhotonView>().RPC("SetNickname", RpcTarget.AllBuffered, nickname);
}
}
gaut
  • 5,771
  • 1
  • 14
  • 45
  • So if your PhotonNetwork.Instantiate returns a game object. It has a transform. And a transform has a position. – BugFinder Aug 08 '23 at 22:47
  • it returns a GameObject but not a MonoBehaviour, and I have the error mentioned in Visual Studio indicating that there is no transform method. `public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, byte group = 0, object[] data = null) {` By the way any feedback on downvotes is appreciated. – gaut Aug 08 '23 at 22:54
  • 1
    A `MonoBehaviour` is attached to a `GameObject`, and will always have gameObject and transform properties. The `Player` class looks like its in the `Photon.Realtime` namespace, and probably inherits from `Object`, and therefore would not have these properties. The error is not from you spawning a gameObject through instantiate, rather it comes from the `Player` class which does not have these properties. `GameObject` does have a `transform` property, ref [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html) – hijinxbassist Aug 08 '23 at 23:30

0 Answers0