Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding data to a list from another script in unity c#

First time using Stackoverflow, thanks very much in advance for any help or examples. The aim is to fill a list called 'playerList' with 'PlayerTank' objects from another script.

I've created a list called 'playerList' in a script called 'PlayerList'

In this I also created the subclass'PlayerTank' with a constructor. The aim is to fill 'playerList' with 'PlayerTank' objects. Heres the code.

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

public class PlayerList : MonoBehaviour {

    public List<PlayerTank> playerList;

    public class PlayerTank { // Here is my class for PlayerTank

        public int playerNumber;
        public int playerPadNumber;
        public string playerName;
        public string playerColour;


        // This is a constructor to make an instance of my class. 

        public PlayerTank (int newPlayerNumber, int newPlayerPadNumber, string newPlayerName, string newPlayerColour) 
        {
            playerNumber = newPlayerNumber;
            playerPadNumber = newPlayerPadNumber;
            playerName = newPlayerName;
            playerColour = newPlayerColour;
        }


    }


void Start () {

        playerList = new List<PlayerTank>();

    }

    }
}

The above works fine and dandy.

However, In a completely separate script called 'Test' I am trying to now add playerTank objects into this list.

None of the below gets even close to working but I'm including it so it might give some insight as to my thought process!

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


public class Test : MonoBehaviour {

    public GameObject playerData;
    PlayerList playerList;
    PlayerTank playerTank;



    // Use this for initialization
    void Start () {
        playerList = playerData.GetComponent<PlayerList> ();
        playerTank = PlayerList.PlayerTank;

        playerList.playerList.Add (new playerTank.playerTank(1,2,"a player name", "black")); 
    }

}

Of course none of the above works! Thanks very much for looking!

like image 650
Jim Avatar asked May 30 '26 06:05

Jim


1 Answers

First of all welcome on StackOverflow. In order to do what you want you need to attach those scripts to a game object in your hierarchy (preferably two). There are two solutions for this: the first one is to declare PlayerList as a public field of Test and assign it through the UI (drag and drop the GameObject that holds the PlayerList script onto the field definition in the Test GameObject:

enter image description here

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


public class Test : MonoBehaviour {

    public PlayerList playerList; //replace GameObject with PlayerList 

    // Use this for initialization
    void Start () {

        var playerTankList = playerList.PlayerTankList; // You can access it directly

        playerTankList .Add (new PlayerList.PlayerTank(1,2,"a player name", "black")); 
    }

}

Remember to add the attribute [Serializable] to the class PlayerList to see the field declared in Test in the UI.

Second solution is more straightforward. You just use a Unity function to retrieve a specific Type associated with a GameObject present in your hierarchy. The function is called FindObjectOfType(); Your Test class in this second solution would look like this:

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


public class Test : MonoBehaviour
{ 
    // Use this for initialization
    void Start()
    {
        var playerList = FindObjectOfType<PlayerList>();

        playerList.PlayerTankList.Add(new PlayerList.PlayerTank(1, 2, "a player name", "black"));
    }

}

EDIT:

I rewrite and add your original PlayerList class here below. I changed the Start function with Awake, which in the Unity chain of events it is called before Start. The reason why you still see NullReferenceException is because the Start events are not called in any specific order. So the Start event in the Test class could occur before the Start event in the PlayerList class. However I tested it and it works if you correctly associate the GameObjects:

PlayerList gameobject correctly set

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

[Serializable]
public class PlayerList : MonoBehaviour
{
    public List<PlayerTank> PlayerTankList;

    public class PlayerTank
    { // Here is my class for PlayerTank

        public int playerNumber;
        public int playerPadNumber;
        public string playerName;
        public string playerColour;


        // This is a constructor to make an instance of my class. 

        public PlayerTank(int newPlayerNumber, int newPlayerPadNumber, string newPlayerName, string newPlayerColour)
        {
            playerNumber = newPlayerNumber;
            playerPadNumber = newPlayerPadNumber;
            playerName = newPlayerName;
            playerColour = newPlayerColour;
        }
    }


    void Awake()
    {
        PlayerTankList = new List<PlayerTank>();
    }

}

hope it helps.

like image 188
codingadventures Avatar answered May 31 '26 18:05

codingadventures



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!