accessing vector 3 List from another scene

Hello,
i’m trying to access vector 3 list from another scene and assign it’s values to multiple game objects positions.
the code is working well, but it gives me this error:

NullReferenceException: Object reference not set to an instance of an object
test1.Update () (at Assets/Script/Scene choos pieces/test1.cs:10)
the code:
1- code which set the values in the first scene:

public class SGPositions : MonoBehaviour {

    public GameObject[] pieces;
    public List<Vector3> piecesPos;

    void Update () {

        piecesPos = new List<Vector3>();
        foreach (GameObject go in pieces) {
            piecesPos.Add (go.transform.position);

        }
    }
}

2- code attached to a game object in the two scenes:

public class test1 : MonoBehaviour {

    public List<Vector3> piecesPosTest1;

    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }
      
    void Update () {
        piecesPosTest1 = GameObject.Find ("scene1 GmObj").GetComponent<SGPositions>().piecesPos;

    }
}

3- code which get the values from the first scene and assign them to game objects positions:

public class SetPieces : MonoBehaviour {
  
    public GameObject[] playerShips;               
    public List<Vector3> piecesPosSetPieces;

    void Start(){

    piecesPosSetPieces = GameObject.Find ("scene1,2 GmObj").GetComponent<test1>().piecesPosTest1;

    setallpieces ();
          
        }

    void setallpieces() {

        for(int i = 0; i < 2; i++) {
            playerShips [i].transform.position = piecesPosSetPieces [i];
        }
    }
}

sorry for my bad english, please HELP.

NullReferenceException: Object reference not set to an instance of an object
test1.Update () (at Assets/Script/Scene choos pieces/test1.cs:10)

So that tells you the error is on line 10 of test1.cs, which is where you try to find and access the object. Break it apart and figure out if GameObject.Find() is actually finding the object. Then check if GetComponent() is finding the SGPositions script. And finally, check if the points are null before accessing them.

Your SGPositions script is also assigning the points every Update, you should probably only be doing it in Awake/Start or whenever the data changes.

Checking .Find() and .GetComponent() has no problem, about checking the points if they null do you mean this:

        if (GameObject.Find ("Grid").GetComponent<SGPositions> ().piecesPos != null) {
            piecesPosTest1 = GameObject.Find ("scene1 GmObj")).GetComponent<SGPositions> ().piecesPos;
        }

Check this solution, i decided to get ride of the object annoying me:) :):sunglasses::sunglasses:

public class test1 : MonoBehaviour {

    public List<Vector3> piecesPostest1;


    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }
       
    void Update () {

        if (Input.GetMouseButtonUp(0)) {
            piecesPostest1 = GameObject.Find ("scene1 GmObj").GetComponent<SGPositions> ().piecesPos;
        }

        Scene curscene = SceneManager.GetActiveScene ();
        string curscenename = curscene.name;
        if (curscenename == "scene1"){
            Destroy (gameObject);
        }
    }
}
1 Like