- Home /
Copying Array from one script to another during runtime?
I have run into a strange issue working with arrays.
I have two scripts. Script A is attached to a PREFAB that is not in the scene until runtime. Script A simply has an array of gameobjects called playerDeck. I use the inspector and set the array to 30. I then populate it with PREFABS until all 30 slots are filled.
Script A:
public GameObject[] playerDeck;
Moving onto script B. I have this script attached to a different gameobject that is there from the start and is NOT created on runtime.
Same deal, an array of prefabs set to 30 in the inspector but left all empty. I also have a gameobject set to have an instance of the player's prefab I fill the playerPrefab slot in the inspector by using the PREFAB named "player" This "player" has a script called playerScript.cs(Script A in this case)
I then call the line where I want to copy the deck, but nothing seems to copy over. The array for the second object doesn't copy anything over and is left blank.
Script B:
public GameObject[] masterDeck;
public GameObject playerPrefab;
masterDeck = playerPrefab.GetComponent<playerScript>().playerDeck;
What am I doing wrong here? This is setting me back quite a bit. I suspect the issue is because I am using a prefab...is there any way around this?
EDIT: the object is instantiated. Script A is instantiated at runtime. I am using PUN(photon unity networking) to instantiated them. So I am not using the standard unity instantiation. could this be what is cause the problems?
Answer by Chris333 · Jan 31, 2015 at 08:12 AM
Hi,
you have to Instantiate the playerPrefab first and than you can get the playerDeck as you want.
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
E.g.:
Instantiate(GameObject playerPrefab, new Vector3(0.0f,0.0f,0.0f), Quaternion.identity);
masterDeck = playerPrefab.GetComponent<playerScript>().playerDeck;
They are indeed instantiated. Forgot to mention that. Sorry!
Is the array with the prefabs you assign in the inspector which you want to copy also empty at runtime?(Script A - playerDeck)) Did you checked that.
Yes I've checked that while the game is running. They are populated. Not empty at all. I am using PUN to instantiate them though...do you think that could be the problem?
Is that unity photon networking? I have no experience in that. You could check if your problem still exist without using photon.
Yes this is using photon. I kinda need it since it's an online game I'm making. I will try it with objects that don't use photon and see if that works at all.
Your answer
Follow this Question
Related Questions
Copy Childen Of GameObjects From Array 1 To Array 2 1 Answer
Using an int to find the index of a GameObject array 1 Answer
How to copy part of an array 2 Answers
Accessing next Camera from array 1 Answer
Public Array of GameObjects 2 Answers