Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Passing data between scenes and ScriptableObject help

Discussion in 'Scripting' started by gasper133, Feb 1, 2016.

  1. gasper133

    gasper133

    Joined:
    Nov 5, 2015
    Posts:
    31
    Hello guys!

    I have two questions today if anyone wants to spend a minute answering them :)

    First problem is, how can I post some data from scene A to scene B? Now I'm in scene A and I choose some things and after I want to go to scene B and program needs to know how those things were chosen (they can be stored inside int[] if it helps :) )

    Second thing, if anyone knows ScriptableObject asset. I'm using scriptable object factory (http://www.tallior.com/unity-scriptableobject-factory/) and I need to have around 150 scriptable object, each with his own data. Now how can I do this most racionally? Right now I've made 150 scriptable objects, and a dictionary, So if I want to acess scriptable object #146, I call function inside Dictionary pass it an int and it returns me the scriptable object I want to acess. (Lot's of IF sentences). Is there any better or "more correct" way to do this? (Excuse me, I'm kind of newbie in unity 3d)

    Thanks for your time guys! :)
     
    osamansr2o1oo likes this.
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    for your first problem you can store data in an external file and load the data for each scene or you can use playerPrefs to save simple data that all scenes can access. The easiest solution would be to create an empty gameObject with a script component that hold all the data you want to be accessible from other scenes then use the DontDestroyOnLoad method in the awake function of the script. This will prevent it from being destroyed when new scenes are loaded.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void Awake() {
    6.         DontDestroyOnLoad(transform.gameObject);
    7.     }
    8. }
    Im not familiar with that asset your using for your second problem. not sure exactly what you are asking.
     
    shekalo likes this.
  3. gasper133

    gasper133

    Joined:
    Nov 5, 2015
    Posts:
    31
    Thanks for the trick! :D

    About second question: Those scriptable object are quite big... They all contain lot's of information and each it's own picture. To be precise, they contain data and picture of cards. And I only need those cards when I'm inside of game, and even there I do not always need all 150 cards. I only need let's say 20 of them (each time other 20, depends of how a player build their deck).

    So the question is: Is there any way, that program doesn't need to read all 150 cards each time player enters game, and can read the card only when certain cards need to be readed? It will be a big waste of time, to read/download all 150 cards with their pictures and data each time, if only few of them will be needed.

    I'm apologizing for my bad English. I hope anyone of you get the point :)
     
  4. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    If the cards are Prefab assets you can place the into a folder in your project named Resources. any asset in a folder named Resources can be loaded independently of the scene at runtime so you dont have to store all 150 cards in memory for each game you can just load whichever card you need by name using Resources.Load.
     
  5. gasper133

    gasper133

    Joined:
    Nov 5, 2015
    Posts:
    31
    So it's okay if those things are ScriptableObjects and not prefabs?
     
  6. osamansr2o1oo

    osamansr2o1oo

    Joined:
    Sep 10, 2015
    Posts:
    9
    hello @gasper133 ,, I was trying to make a card game as well but I faced some design problems and couldn't continue working on it ,, it would be awesome if we can talk ,, if not, for any reason, could u at least tell us what did u do about ur 2nd Question ?

    the only use I know to ScriptableObjects is this

    I'm sure there is more to them

    also if anyone is making a card game and would like to talk about there programming design, approaches and over all progress ,, I'd be very delighted to talk with u ^_^ cheers!

    P.S. I'm not used to using the Forums, and sorry if I just miss used it ;p