Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by BeyondTheHorizons · Sep 28, 2020 at 10:47 AM · script.scenesstats

Referencing between Scenes,Accessing data only from main scene

Hi guys ! I am beginner game developer.

My English is poor but hopefully it makes sense

I am making 2D game where I have

Mainscene (contains "stats" code) & battlescenes (1,2,3....)

Mainscene displays current stats with text.

upon my player contacting an enemy during the main scene, it will direct you to assigned battle scene. From there, by clicking an option (made with button UI), it will increase/decrease stat as it directs you back to the main scene. The mainscene now would have updated stats from your choice earlier.

I got few things to work but I just cant reference the "stats" code in the battle scene as this script is included in the main scene.

Here is my stats code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Stat : MonoBehaviour {
 
     [SerializeField] Text textMp;
     [SerializeField] Text textSuspicion;
     [SerializeField] Text textPersuasion;
     [SerializeField] Text textHp;
 
     static int MaxMp= 100;
     static int MaxSUSPICION = 100;
     static int MaxPERSUASION = 50;
     static int MaxHP = 100;
 
      int currentMp = 100;
      int currentSuspicion = 0;
      int currentPersuasion = 0;
      int currentHp = 100;
 
     void StartStat () {
         textMp.text = currentMp.ToString ();
         textSuspicion.text = currentSuspicion.ToString ();
         textPersuasion.text = currentPersuasion.ToString ();
         textHp.text = currentHp.ToString ();
     }
 
     private void Start () {
         StartStat ();
     }
 
     public void StatManager (int Blood, int Suspicion, int Persuasion, int Hp) {
         if (currentMp <= MaxMp) {
             currentMp += Mp;
             if (currentMp > MaxMp) currentMp = MaxMP;
         }
         if (currentSuspicion <= MaxSUSPICION) {
             currentSuspicion += Suspicion;
             if (currentSuspicion > MaxSUSPICION) currentSuspicion = MaxSUSPICION;
         }
         if (currentPersuasion <= MaxPERSUASION) {
             currentPersuasion += Persuasion;
             if (currentPersuasion > MaxPERSUASION) currentPersuasion = MaxPERSUASION;
         }
         if (currentHp <= MaxHP) {
             currentHp += Hp;
             if (currentHp > MaxHP) currentHp = MaxHP;
         }
         bsphText ();
     }
     void bsphText () {
         textMp.text = currentMp.ToString ();
         textSuspicion.text = currentSuspicion.ToString ();
         textPersuasion.text = currentPersuasion.ToString ();
         textHp.text = currentHp.ToString ();
     }
 
 }


Below is my one of the option code from the battle scene

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement; 
 
 public class dialogue1 : MonoBehaviour
 {
    public void option1(){
        var op1 = GameObject.Find("statslover").GetComponent<Stat>();
        op1.StatManager(-2,2,2,-2);
        SceneManager.LoadScene("Mainscene");
    }
 }
 

The script Stat is under statslover object.

I tried to use static with stats script but it gives me error. I tried dontdestroyonload method and it works but problem is that since it saves the text as well, it will override the battlescene making the buttons unable to click.

Please kindly help me do what I am thinking in my head

Thank you so much everyone

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RPG instatiating the player 2 Answers

how can we find, in which all places a same script have been used in the project 1 Answer

Have problem with camera lerp moving 0 Answers

Applying Scripts held in a list to a game object 1 Answer

How do I call a class' variable from a script on one object, from a script on another gameobject? The class/variables auto-complete in the code, but won't work in game. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges