need help transferring something from 1 scene to the other
Hi I'm trying to bring a script that enables gameobjects (stars) in the canvas from one scene to the other, upon level completion. What I mean by this is: -Player gets a score in the level and completes it -the score is then taken to a new scene and placed on a level complete ui (it's a new scene because of a problem I had, listed at the bottom of this post) -the script shown below takes effect, displaying 1-3 stars based on performance (think of candy crush and similar mobile games) -the script itself counts how many collectable items are in the level and how many have been collected. It then uses this information to make a percentage out of it and award stars based on the percentage collected by enabling the corresponding gameobject(s)
Problem is I have no idea how to do this. It's worth noting that I've only been using unity for 2 weeks or so, so I'm still really new to it all. Script is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelStars : MonoBehaviour
{
public GameObject[] stars;
private int coinsCount;
private int diamondCount;
private int starCount;
private void Start()
{
coinsCount = GameObject.FindGameObjectsWithTag("Coin").Length;
diamondCount = GameObject.FindGameObjectsWithTag("Diamond").Length;
starCount = GameObject.FindGameObjectsWithTag("Star").Length;
}
public void StarsAchieved()
{
int coinsLeft = GameObject.FindGameObjectsWithTag("Coin").Length +
GameObject.FindGameObjectsWithTag("Diamond").Length +
GameObject.FindGameObjectsWithTag("Star").Length;
int coinsCollected = coinsCount + diamondCount + starCount - coinsLeft;
float percentage = float.Parse(coinsCollected.ToString()) / float.Parse(coinsCount.ToString()) * 100f;
if (percentage >= 0f && percentage < 33)
{
Debug.Log("You suck!");
}
else if (percentage >= 33f && percentage < 66)
{
stars[0].SetActive(true);
Debug.Log("You got 1 star.");
}
else if (percentage >= 66f && percentage < 99)
{
stars[0].SetActive(true);
stars[1].SetActive(true);
Debug.Log("You got 2 stars!");
}
else if (percentage >= 100)
{
stars[0].SetActive(true);
stars[1].SetActive(true);
stars[2].SetActive(true);
Debug.Log("M-m-m-MAXIMUM SCORE!!!!!111");
}
}
}
My original problem was trying to make the score from the game carry over to a level complete screen within the same scene. I couldn't get it working so I found a workaround that involved making another scene and transitioning to it, which fixed my problem using playerprefs. However, in doing this, my LevelStars script is now completely broken. Ive tried dontdestroyonload to move the gameobject containing the script and array, but all the reference objects are ''missing'' as a result.
I've also looked into playerprefs but I don't know how to accomplish it, if its even possible.
Please help! I'm pulling my hair out over this :)
I
Okay so I fixed my problem by solving my initial problem with the score text and abandoning the idea of another scene etc.
I am still seeking a solution for this though, because I want to save the value of the stars earned and retrieve it on the level select screen to show the stars earned for that level. Anybody able to shed some light on this?
Your answer
Follow this Question
Related Questions
change a created transform in a script from another one 1 Answer
I have a problem about my Arkanoid game. Please help! 0 Answers
how reload scene when rotate screen android 1 Answer
Problem with script in multiple scenes needing to refer to FPS Controller. 1 Answer
Complicated level change problem 1 Answer