0

Need some expertise on this one. I am currently Spawning a player from a class in a way where I create the object and then define the variables line by line, like this:

Player p = new Player();
p.Avatar = go;
p.PlayerName = playerName;
p.ConnectionId = cnnId;
p.Avatar.GetComponentInChildren<TextMesh>().text = pName;
players.Add(cnnId, p);

and the player class was a simple little bit structured like this:

public class Player
{
    public string PlayerName;
    public GameObject Avatar;
    public int ConnectionId;
}

So that worked, but I wanted to expand the arguments and use a constructor to create my player object which I tried to do by creating my object like this instead:

Player p = new Player(playerName, go, cnnId); p.Avatar.GetComponentInChildren<TextMesh>().text = pName; players.Add(cnnId, p);

and then I created my constructor like this:

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

public struct Player {

    Client client;

    public string PlayerName { get; set; }
    public GameObject Avatar { get; set; }
    public int ConnectionId { get; set; }
    public byte[] Tex { get; set; }
    public string Type { get; set; }
    public string Id { get; set; }
    public int Strength { get; set; }
    public int Hitpoints { get; set; }
    public bool IsAlive { get; set; }

    // Initial method takes base arguments for testing
    public Player(string playerName, GameObject avatar, int connectionID) : this()
    {
        this.PlayerName = playerName;
        this.Avatar = avatar;
        this.ConnectionId = connectionID;

        client.infoDisplayText.GetComponent<Text>().text += playerName + " " + ConnectionId + " " + "\n";
    }

    // Overload method takes all player arguments
    public Player(string playerName, GameObject avatar, int connectionID, byte[] tex, string type, string id, int strength, int hitpoints, bool isAlive) : this()
    {
        this.PlayerName = playerName;
        this.Avatar = avatar;
        this.ConnectionId = connectionID;

        this.Tex = tex;
        this.Type = type;
        this.Id = id;
        this.Strength = strength;
        this.Hitpoints = hitpoints;
        this.IsAlive = isAlive;

        Debug.Log(id + " : " + type + " created with strength " + strength + ", hit points " + hitpoints + ", and a texture the size of " + tex.Length);

        client.infoDisplayText.GetComponent<Text>().text += playerName + " " + id + " : " + type + " created with strength " + strength + ", hit points " + hitpoints + ", and a texture the size of " + tex.Length +" \n";
    }
}

When I try to use the constructor method though it doesn't work - my player name doesn't appear and my player movement doesn't get updated across the network anymore. What changes do I need to make to get my constructor to work?

Complete function that I'm using to Spawn my player:

private void SpawnPlayer(string pName, int cnnId)
    {
        GameObject go = Instantiate(playerPrefab) as GameObject;

        // Is this our player?
        if (cnnId == ourClientId)
        {
            // Add mobility
            go.AddComponent<Movement>();    // Add Movement.cs Script

            // Remove Connect Button
            if(GameObject.Find("Canvas").activeInHierarchy == true)
                GameObject.Find("ConnectButton").SetActive(false);

            isStarted = true;
        }

        Player p = new Player(playerName, go, cnnId);
        p.Avatar.GetComponentInChildren<TextMesh>().text = pName;
        players.Add(cnnId, p);
    }
3
  • Why are you using a struct at all? if you switch to a class does it fix your problems? Commented Oct 14, 2017 at 21:44
  • 1
    Are you sure that you are not getting any NullReferenceException, especially when setting the avatar GameObject? Commented Oct 14, 2017 at 21:46
  • @ScottChamberlain switching to a class doesn't seem to fix the problem. Commented Oct 14, 2017 at 21:49

1 Answer 1

1

When I try to use the constructor method though it doesn't work - my player name doesn't appear and my player movement doesn't get updated across the network anymore. What changes do I need to make to get my constructor to work?

First of all, this doesn't have anything to so with the constructor. Notice that your working Player is a struct and the none working one is a class. This really shouldn't make any difference.

Here are two possible reasons why your new class is not being updated:

1.The problem is the use of { get; set; } in that class. Unity cannot serialize/de-serialize auto property. Remove them from each variable in that class and it should work.

2.Also, you are not initializing the client variable in that class. Initialize it before using it or performing client.infoDisplayText.GetComponent<Text>() on it.

Sign up to request clarification or add additional context in comments.

3 Comments

Removing { get; set; } didn't fix the issue unfortunately. I posted the entire function that I'm using to spawn my player objects above. Is there something else in the there that I have wrong perhaps?
Please read the second part of my answer abut using a variable that you did not initialize....
Sorry I missed that part. I initialized the variable, everything is working. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.