Hi there. I’ve been having some problems with passing values from objects that originate from different scenes.
To specify accurately,
I’ve got a login scene with an object called ‘Player’ with a script on it that handles information (login, getScore, saveScore etc.) via functions that call certain stored procedures on an SQL server.
This object is then passed through scenes (main menu, level 1, level 2 etc.) using DontDestroyOnLoad(this);
On scene “level_1” I’ve got a trigger object (start / finish) which keeps track of time. When a player passes through finish there is a variable called finishTime which holds the time it took to complete level.
I’d like to pass this value to the Player object so I can call a function to save the score. Normally, it wouldn’t be a problem to pass a variable value if both objects were placed to the scene by me. Then I could manually target the object I want to drag values from. But this is not the case.
I’ll post code relevant to this situation.
Player.cs (created on login scene)
using UnityEngine;
using System;
using System.Collections;
public class Player : MonoBehaviour
{
public string username;
public IEnumerator SaveScore(string scr)
{
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("score", scr);
WWW download = new WWW("http://www.colorsoft.hr/Portals/ColorsoftHr/files/SaveScore.aspx", form);
yield return download;
if (download.error != null)
{
outputText = download.error;
}
}
}
Trigger.cs
public class CircuitTimer : MonoBehaviour
{
public double levelTime;
public double difference;
public double actualTime;
public double finishTime;
public bool started = false;
public bool finished = false;
// Use this for initialization
void Start ()
{
actualTime = 0;
}
// Update is called once per frame
void Update ()
{
levelTime = Time.time;
if(started)
actualTime = levelTime - difference;
}
//
void OnTriggerEnter (Collider other) {
if(started)
{
finishTime = actualTime;
finished = true;
}
difference = Time.time;
started = true;
}
}
So, what I want is: when finished is true then pass the value of finishTime to object Player and use that value in function SaveScore().
My scripts work fine, there are no compilation errors yet for some reason, the values wont pass. I’m sure I’m probably missing a tiny bit of knowledge to connect all this.
Any help would be greatly appreciated. Maybe I’m using a completely wrong method(ology). Really can’t tell. I’m counting on your will to help us n00bs.
Cheers!