Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by skyx26 · Aug 03, 2015 at 08:18 AM · c#unity 4.6

Realoding an scene doesn't load the content of a variable from another script

Hi guys, I need help solving a problem I just find while scripting for Unity3D.

This is my case:

I have 5 cards on a table. One card will add a point to the score while the other 4 will take a life away. The right card is ramdomly choose on a script attached to a different object present on the scene.

In my first card I have this script attached

 public void SelectCard()
 {
     switch (externalVariable)
     {
         case 1:
             UnityEngine.Debug.Log("You win a point!");
             Application.LoadLevel("Mini Game 1");
             break;
 
         case 2:
             UnityEngine.Debug.Log("You lose a life!!!");
             Application.LoadLevel("Mini Game 1");
             break;
 
         case 3:
             UnityEngine.Debug.Log("You lose a life!!!");
             Application.LoadLevel("Mini Game 1");
             break;
 
         case 4:
             UnityEngine.Debug.Log("You lose a life!!!");
             Application.LoadLevel("Mini Game 1");
             break;
 
         case 5:
             UnityEngine.Debug.Log("You lose a life!!!");
             Application.LoadLevel("Mini Game 1");
             break;
     }
 }

As you can see, if the random number is one the player will score one point if he choose the first card, but if the random number is different and the player choose the first card, he will lose a life.

The random number is stored on a int variable. To acces it, I add this to the start() section of the script attached on the first card

 externalVariable= externalObject.GetComponent<MiniGame1RightCard>().rightCard;
 UnityEngine.Debug.Log("The right card is card number: " + externalVariable);

So, the first card will access the random number generated by the external script attached to a different object present on the same scene, store it, and then tell me by console output which one is the right card. Then, when the player clic the first card, the script will compare the random number stored on externalVariable to check if the first card is actually the right card or not. Either way, the last step is reload the same scene to start all over again.

The very fist time the scene is loaded everything runs perfect. My problem start with the second time the scene is loaded: externalVariable is always zero. Now for the tricky part, if I put the very same code on the Update() section instead of the Start() section, for one or two seconds the externalVariable stays at zero and then change for the random number generated.

My guess is, I'm doing something wrong when I reload the scene to start again, but I don't know what.

Any ideas about why the code works fine the very first time on the Start() section and every single time on the Update() section???

UPDATE

The whole code attached to card1

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.IO;
 using System;
 using System.Threading;
 
 public class Card1: MonoBehaviour
 {
     private string querySeleccionar = "SELECT Nivel FROM LevelAndDifficultyWHERE id=1;";
     Text textoSilaba1;
     public GameObject cardImage1;
     public int level;
     public int letra1;
     public int silaba1;
     public int externalVariable;
     public GameObject objetoExterno;
 
     public void SeleccionarSilaba()
     {
         switch (externalVariable)
         {
             case 1:
                 UnityEngine.Debug.Log("You win a point!");
                 Application.LoadLevel("Mini Juego 1");
                 break;
 
             case 2:
                 UnityEngine.Debug.Log("You lose a life!!!");
                 Application.LoadLevel("Mini Juego 1");
                 break;
 
             case 3:
                 UnityEngine.Debug.Log("You lose a life!!!");
                 Application.LoadLevel("Mini Juego 1");
                 break;
 
             case 4:
                 UnityEngine.Debug.Log("You lose a life!!!");
                 Application.LoadLevel("Mini Juego 1");
                 break;
 
             case 5:
                 UnityEngine.Debug.Log("You lose a life!!!");
                 Application.LoadLevel("Mini Juego 1");
                 break;
         }
     }
 
     public void OnMouseDown()
     {
         
     }
     
     // Use this for initialization
     void Start ()
     {
         //lot of database stuff

         switch (nivel)
         {
             case 1:

             //lots of cases
         }
         externalVariable= externalObject.GetComponent<MiniGame1RightCard>().rightCard;
         UnityEngine.Debug.Log("The right card is card number: " + externalVariable);
         }
 
 // Update is called once per frame
 void Update ()
     {

 }
 }


The script that generate the random number

 using UnityEngine;
 using System.Collections;
 
 public class MiniGame1RightCard: MonoBehaviour
 {
     public int rightCard;
 
     // Use this for initialization
     void Start ()
     {
         silabaCorrecta = UnityEngine.Random.Range(1, 5);
         UnityEngine.Debug.Log("The right card is card number " + rightCard);
     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
 }
Comment
Add comment · Show 8
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
avatar image KdRWaylander · Aug 03, 2015 at 08:52 AM 0
Share

Can we have the script that generates 'rightCard' ?

avatar image skyx26 · Aug 03, 2015 at 10:07 AM 0
Share

@$$anonymous$$dRWaylander Updated the description with the whole code.

avatar image KdRWaylander · Aug 03, 2015 at 12:18 PM 2
Share

try to use Awake() ins$$anonymous$$d of Start() in generating the random number ?

avatar image barbe63 · Aug 03, 2015 at 04:48 PM 2
Share

Yes try that or change the script execution order. I will add that:

 silabaCorrecta = UnityEngine.Random.Range(1, 5);

will never generate 5, use (1,6) range ins$$anonymous$$d.

By the way in your code the variable you are randomizing have a different name that the on you use after but I guess you simply forgot to translate it for us.

avatar image barbe63 · Aug 04, 2015 at 06:58 PM 1
Share

For the record it is inclusive with the float version and exclusive with the int version: http://docs.unity3d.com/ScriptReference/Random.Range.html

If you scroll down you will see it says all that ;)

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

unityengine.animator does not contain a definition for parameters 0 Answers

Can someone help me please. error CS1519 1 Answer

Making a bubble level (not a game but work tool) 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