Instantiate object with class values from other script

Hi!

I have 2 scripts,

My Balance,cs which is for instantiating my object. And a login class where my login is being handled.

Now this is how my instantiate looks like:

    void OnLevelWasLoaded()
    {
        GameObject thisPlayer = PhotonNetwork.Instantiate(playerPrefab.name, Vector3.up * 5, Quaternion.identity, 0) as GameObject;
        var tP = thisPlayer.GetComponent<PlayerClass>();
        tP.GetComponent<PlayerClass>().Name = "test2404";
        tP.GetComponent<PlayerClass>().Score = 1000;
        thisPlayer.transform.FindChild("FirstPersonCharacter").gameObject.SetActive(true);
    }

It works. perfectly,

but what i would like, to add at the .Name and .Score is the values i retrieve from a database like. I retrieve them already in my MainLogin.cs:

    IEnumerator getCharacters()
    {
        WWWForm Form = new WWWForm();
        Form.AddField("Email", email);
        Form.AddField("Password", password);

        WWW GetItemsWWW = new WWW(charactersURL, Form);
        yield return GetItemsWWW;
        if (GetItemsWWW.error != null)
        {
            Debug.LogError("Cant connect to character selection!");
        }
        else
        {

            string LogText = GetItemsWWW.text;
            //Debug.Log(LogText);
            string[] LogTextSplit = LogText.Split(',');
            for (int i = 0; i < LogTextSplit.Length; i++)
            {
                Debug.Log(LogTextSplit[i]);
                CH.showCanv(LogTextSplit);
            }
            CharSelCanv.SetActive(true);
        }

    }

Inside of LogTextSplit is the character name.
How can i manage to retrieve the LogTextSplit inside of my Balance.cs? The first code i showed you, i cant figure this out :confused:

Thanks alot for any help!

Well, first, I think you need to figure out the timing of all this. Your login code appears to be a coroutine, and fetches data from the web, so getting that LogTextSplit data is going to happen well after the start of the scene. Yet your Balance.cs is instantiating something as soon as the level is loaded.

So, unless Balance.cs runs in some later scene than where MainLogin.cs runs, there is no way that data is going to be available by the time it’s instantiating stuff.

So maybe instead of instantiating things as soon as the level is loaded, you want to wait and do it after login? In that case, here’s how I would do it:

  • Make a public method in Balance.cs that does this instantiation.
  • Make a public UnityEvent in MainLogin, and invoke that at the end of the login coroutine.
  • Hook the event up to the Balance instantiation method in the inspector.

If you’re not familiar with Unity events, here’s a quick tutorial I did on them a while back.

Well, the mainlogin has a DontDestroyOnLoad, balance is indeed being ran in a later scene, where MainLogin is still active

Ah, OK then, that’s different.

In that case, all you really need to do is store your login data someplace easily accessible. For example, some public static fields, declared like so:

public class MainLogin : MonoBehaviour {
    public string LogText;
    public string[] LogTextSplit;
    ....

And then store to these rather than to local variables in your coroutine. Then in your other script, you can reference those fields directly, as MainLogin.LogText or MainLogin.LogTextSplit.

Aah that looks promising!

Using this:

public static string[] LogTextSplitHolder;

in MainLogin
at the login i set LogTextSplitHolder = LogTextSplit;
but when i go into my balance.cs:

        tP.GetComponent<PlayerClass>().Name = MainLogin.LogTextSplitHolder;

It (ofcourse) says that it cant convert from
string[ ] to string

do i have to use:

        tP.GetComponent<PlayerClass>().Name = MainLogin.LogTextSplitHolder[0];

then? this does work, but i dont know if i am doing it in the proper way

Well I don’t know what your intent is. You seem to have an array of strings in one place, and want just a single string in the other place. Which string do you want? Only you can say.

1 Like

Not really related to the issue, joe already covered that.

tP.GetComponent<PlayerClass>()

tP is already set to an instance of PlayerClass and does not need to be repeated.

1 Like

I need the single one, guess thats changing only 1 line haha. Thanks a bunch!

You are right! :stuck_out_tongue:

1 Like