Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A variable that can be used in 2 scripts?

Discussion in 'Scripting' started by 123fendas, Nov 28, 2015.

  1. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Let's say I want to use

    Code (CSharp):
    1. public int variable = 1;
    as a variable. A keycode sets it to 2.

    Then, in another script:

    Code (CSharp):
    1. if (variable == 2)
    2. {
    3.      something;
    4. }
    How can I use a variable in 2 scripts?

    EDIT 1/4-17: It was with this thread that I realized that I'm never going to be able to make a game I'm satisfied with.
     
    Last edited: Apr 1, 2017
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    GetComponent<MyScriptName>() will grab reference to a other script object so you can access its public vars
     
    Kiwasi likes this.
  3. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Code (CSharp):
    1. GetComponent<press_start>();
    It doesn't work.


    press_start.cs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class press_start : MonoBehaviour {
    5.     //Sound
    6.     public AudioClip startup;
    7.     private AudioSource source;
    8.     public AudioClip titleScreenMusic;
    9.     private AudioSource source2;
    10.     //Other
    11.     public Sprite regular;
    12.     private SpriteRenderer sprite_renderer;
    13.     public int press_enter = 0;
    14.     //Time
    15.     public float timeRemaining = 5;
    16.     public int loadHouseLevel = 0;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         //press start graphic
    21.         GetComponent<SpriteRenderer>().sprite = regular;
    22.         sprite_renderer = GetComponent<SpriteRenderer> ();
    23.         //sound (startup)
    24.         source = GetComponent<AudioSource>();
    25.         source2 = GetComponent<AudioSource>();
    26.         source2.PlayOneShot(titleScreenMusic, 1F);
    27.      
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update () {
    32.  
    33.         if (Input.GetKeyDown (KeyCode.Return) && press_enter == 0)
    34.         {
    35.             press_enter = 1;
    36.             sprite_renderer.enabled = false;
    37.             source.PlayOneShot(startup, 1F);
    38.         }
    39.  
    40.         if (press_enter == 1)
    41.         {
    42.             timeRemaining -= Time.deltaTime;
    43.         }
    44.  
    45.         if (timeRemaining < 0)
    46.         {
    47.             Application.LoadLevel("path");
    48.             loadHouseLevel = 1;
    49.         }
    50.  
    51.     }
    52.  
    53. }
    cutscene.cs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class cutscene : MonoBehaviour {
    4.     public Sprite home;
    5.     public Sprite inTheMailbox;
    6.     private SpriteRenderer sprite_renderer;
    7.  
    8.     public float timeRemaining2 = 4;
    9.  
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         GetComponent<SpriteRenderer>().sprite = home;
    15.         sprite_renderer = GetComponent<SpriteRenderer>();
    16.  
    17.         GetComponent<press_start>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.         if (loadHouseLevel == 1)
    23.         {
    24.             loadHouseLevel = 2;
    25.         }
    26.  
    27.         if (loadHouseLevel == 2)
    28.         {
    29.             timeRemaining2 -= Time.deltaTime;
    30.         }
    31.  
    32.         if (timeRemaining2 > 0)
    33.         {
    34.             GetComponent<SpriteRenderer>().sprite = inTheMailbox;
    35.         }
    36.     }
    37. }
    38.  
     
  4. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    You need to declare a variable for it first. Basically do it the exact same way you are doing your SpriteRenderer setup.

    public press_start variableName; for instance.

    Note that GetComponent is going to assume your scripts are on the same game object.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This may help. It explains conceptually some of the common ways to communicate between scripts.

     
    Ryiah, elmar1028 and cristo like this.
  6. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Can you be more specific? Can you put the
    Code (CSharp):
    1. code I need to type
    inside a code tag?
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Follow BoredMormon's link. Script-to-script communication is one of the basic skills necessary for Unity, and we'd honestly be doing you a disservice by just providing the line of code you need.
     
  8. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    I've watched the video. I still don't know what to do.
     
  9. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    A good way to initiate yourself to a particular programming technic is to isolate it.

    Make a new project with only 2 scripts corresponding to the core of your problem, keep it as simple as it gets. Then do trials and errors until you've figured it out and are comfortable with it !
     
  10. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    How am I supposed to do that? I'm not the guy that figures something out.
     
  11. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    I took your advice.

    script1:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class script1 : MonoBehaviour {
    5.  
    6.     public int var1 = 0;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.      
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (Input.GetKeyDown(KeyCode.Space))
    16.         {
    17.             var1 = 1;
    18.         }
    19.     }
    20. }
    21.  
    script2:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class script2 : MonoBehaviour {
    5.  
    6.     public int var2 = 0;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         GetComponent<script1>(var1);
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (var1 == 1)
    16.         {
    17.             var2 = 1;
    18.         }
    19.     }
    20. }
    21.  
    Not working.
     
  12. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
  14. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    It's good that you started it, now you also need to learn to read compiler errors.

    Unity is probably telling you that it doesn't know the identifiers lines 10 and 15 in your script2. Following the previously shared link should point you in the right direction to fix it !
     
  15. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    ...I just can't do it.

    script2:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class script2 : MonoBehaviour {
    5.  
    6.     public int var2 = 0;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.        
    11.  
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (var1 == 1)
    17.         {
    18.             var2 = 1;
    19.         }
    20.         script1 otherScript = GetComponent<script1>();
    21.         otherScript.var1();
    22.     }
    23. }
    24.  
     
  16. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Oh okay then... here's an example which you can edit to suit your needs.
    Code (CSharp):
    1. ReadMore nextTime = GetComponent<ReadMore>();
    2. nextTime.something=1;
    If you want to call a function you would do
    Code (CSharp):
    1. nextTime.SomeFunction();
     
  17. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    If it helps, you're getting closer to the right answer. I'm still not going to hand it to you, but let me see if I can give you some pointers in the right direction:

    1) What you're ultimately wanting to do is to modify & access var1 on script1 from script2 - var2 does not need to exist, and it's only confusing the problem. Go ahead and delete that.

    2) Line 20 in the script you posted is exactly right.

    3) When you see someName(), the parentheses on the end mean that it's a function. I'm guessing you copied that code from the samples on the docs page, which are referencing functions on other objects, whereas you are trying to reference a variable on another object. If var1 were a function, you'd have that line correct! Since var1 is not a function, though, that's not how you access it. You can set var1 almost like you are (currently) setting var2, you just need to add the otherScript part to that line.

    Finally, as @_met44 says, learn to use error messages. If you don't know how to see them, look at the very bottom of the Unity window - you can click on this part to open the console and see more information, too. (With your script as it is in your last comment, it will definitely be giving you at least one error, on line 16.) If you double-click on the error in the console, it'll open the script to the line containing the error, and as you get better, you'll learn what the error messages mean. And even if you don't know what they mean, definitely include them when asking for help - they help us find your problems!
     
  18. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class script2 : MonoBehaviour {
    5.  
    6.     public int var2 = 0;
    7.     ReadMore var1 = GetComponent<script1>();
    8.     var1.something = 1;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.      
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.         if (var1 == 1)
    19.         {
    20.             var2 = 1;
    21.         }
    22.      
    23.     }
    24. }
    I still can't get it to work.

    I don't know what "ReadMore" should be replaced with, what "nextTime" should be replaced with, and what "ReadMore" inside '<' these '>' should be replaced with, I don't know what "something" should be.

    And I'm getting an error, line 8, "Unexpected symbol '=' in class, struct or interface member declaration".
     
  19. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Go back to what your script was in the previous comment, it was pretty close to right in that one. Then follow my comment from there.
     
  20. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    What, like this?
    Code (CSharp):
    1. public int otherScript var1;
     
  21. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    No, that's declaring it. I mean setting it, as in this line:
    Code (csharp):
    1. var2 = 1;
     
  22. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Time to put him out of his misery. He has made a valiant effort trying.

    In fairness this was one of the hardest concepts to learn when starting coding. It's all downhill from here.

    Code (CSharp):
    1. public class Script1{
    2.     public int var = 0;
    3. }
    4.  
    5. public class Script2 : MonoBehaviour {
    6.     void Update () {
    7.         GetComponent<Script1>().var = 2
    8.     }
    9. }
    You'll also want to understand the difference between value and reference types. Google it, it matters.
     
    Ryiah likes this.
  23. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    If you're at the point of stress/nervousness that "ReadMore NextTime" in someone's comment isn't clear you should take a break away from the computer. Walking and breathing helps solving issues, go see the sun a little bit if you can too...
     
    StarManta likes this.
  24. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    But in my code
    Code (CSharp):
    1. if (var1 == 1)
    2.         {
    3.             var2 = 1;
    4.         }
    , I still can't use it. Do I have to type, like, (<script1>.var1 == 1) or something?
     
  25. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Assuming this complies it looks like a value versus reference issue.
     
  26. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    This is really good advice, honestly. Everyone's been at this point at some point or other, where the mere stress of not understanding the problem causes you to sort of shut down, and makes it harder to think at all, and a common result is to stab blindly and hope you hit on the right answer - and that's clearly what's happening here. Take a break, close Unity, go on a walk, take a shower, and come back to this in a couple of hours or days. When you come back fresh it'll be much easier to process.
     
  27. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    So, how do I fix it?
     
  28. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    No, I guess I'm just not smart, because today's another day, and it's not clear at all.
     
  29. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'll take a stab at helping out.

    Imagine a folder with a piece of paper in it. On that piece of paper you've written the number 1. When the folder's closed you can't see what's written on the piece of paper, or even how many papers are in the folder.

    So now you've got another folder with another piece of paper in it. Your goal is to open the first folder, read the number that's written on the paper, and write that same number on the paper in the second folder. So how would I effectively explain to you how to do that? I can't just say "Give me the number on the paper" because you don't know what paper I'm talking about, right? It could be the one in the first folder, the one in the second folder, or some other arbitrary folder that you don't even know about. I'd first have to say "Give me the first folder". Once I have the folder I can open it and look at the paper.

    So, in the context of Unity - how do I get the folder I need? First and foremost we have the GameObject. Think of these like filing cabinets - big containers that can hold many folders. We have a bunch of GameObjects in a scene potentially so the first question to ask is "Are both of these folders in the same filing cabinet" or succinctly "Are these two scripts attached to the same GameObject". If they are, we can directly ask for one folder (script) from another one. This is where GetComponent comes in.

    Code (csharp):
    1.  
    2. public class FolderOne : MonoBehaviour
    3. {
    4.     public int PaperValue = 1;
    5. }
    6.  
    7. public class FolderTwo : MonoBehaviour
    8. {
    9.     public int OtherPaperValue;
    10.  
    11.     void GetOtherFolder()
    12.     {
    13.         // this will work if both FolderOne and FolderTwo are
    14.         // on the same GameObject
    15.         OtherPaperValue = GetComponent<FolderOne>().PaperValue;
    16.     }
    17. }
    18.  
    If the folders aren't in the same filing cabinet then we have to answer the question "Which filing cabinet do we need". There are lots of methods for doing this, the most basic of which is to make a new variable in FolderTwo that points to the filing cabinet (GameObject) that we need. To do that we can add a new variable underneath OtherPaperValue in FolderTwo that looks like this
    Code (csharp):
    1.  
    2. public GameObject FilingCabinet;
    3.  
    Then in the Inspector in your scene you can drag and drop the other GameObject onto that slot and change the assignment in GetOtherFolder to this
    Code (csharp):
    1.  
    2. OtherPaperValue = FilingCabinet.GetComponent<FolderOne>().PaperValue;
    3.  
    This is saying - "get me the folder in the declared FilingCabinet" as opposed to "get me the folder in the *same filing cabinet*"
     
  30. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    It looks like I need more help.

    What do I drag and drop into FilingCabinet, the game object? What game object do I drag into that?

     
  31. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The one your FolderOne script is attached to.

    There's also no reason to have 2 audio sources on one GameObject :)
     
  32. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    But where do I find it in the Inspector?
     
  33. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Where do you find what?

    You have to either drag the GameObject from the scene hierarchy to the Inspector or press the little circle icon and pick it from popup window. The same process you used to fill out Startup, Title Screen Music, and Regular....
     
  34. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Your posted code needs to be altered. In Update you are trying to read var1 from script1, but you are doing it before you even know what script1 is. You have to get your script1 component before you can do anything with its variables.

    This was your code:

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     public class script2 : MonoBehaviour {
    5.  
    6.         public int var2 = 0;
    7.  
    8.         // Use this for initialization
    9.         void Start () {
    10.        
    11.  
    12.         }
    13.    
    14.         // Update is called once per frame
    15.         void Update () {
    16.             if (var1 == 1)
    17.             {
    18.                 var2 = 1;
    19.             }
    20.             script1 otherScript = GetComponent<script1>();
    21.             otherScript.var1();
    22.         }
    23.     }
    24.  
    25.  
    If you declare your otherScript at the top, you can get a permanent reference to it, rather than looking for it in every update. You normally don't want to keep calling something like this over and over again in Update.

    In my code below:
    You are declaring what type of object it is at the top(script1), then you are getting a reference to it in Start method(otherScript). Once you have a reference, you can use it. You can do these things in Update or in custom methods, but this is the simplest way to have a permanent reference to your other script.
    If you do this in Update, you are losing your reference and having to get it all over again in every Update cycle.

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     public class script2 : MonoBehaviour {
    5.  
    6.         public int var2 = 0;
    7.         public script1 otherScript;
    8.  
    9.         // Use this for initialization
    10.         void Start () {
    11.            otherScript = GetComponent<script1>();
    12.  
    13.         }
    14.    
    15.         // Update is called once per frame
    16.         void Update () {
    17.             if (otherScript.var1 == 1)
    18.             {
    19.                 var2 = 1;
    20.             }
    21.             // you would use the () below for calling a method, but you won't need it if you are just reading/changing a variable on the script
    22.            // otherScript.var1();
    23.         }
    24.     }
    25.  
    26.  


    You really should just watch some of the training videos on the Unity site for this. It's a really basic concept that is probably covered in quite a few of the tutorial projects. I think the following video link should go over it(I didn't watch it to see exactly):

    http://unity3d.com/learn/tutorials/.../communicating-between-components-gameobjects
     
  35. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Thank you for that circle tip! I think I know what you mean now.

    But here's where the problem is.

    I'm using 2 different scenes. Do I have to do something different?
     
  36. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    But if I put it in void Start, wouldn't that only happen the instant you run the game? If the variable changes, it needs to update it, right? That's why I thought it was logical to put it in the Update void.
     
  37. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    No, in Start, you would simply be getting a reference to the script, not its variables directly. Then later, if you wanted to see its variables, it would look at the reference script for whatever is there at the moment you check.

    So, in Start, it's simply telling you where the script is located. It's not worried about what's inside it. When you call up the script to use its variables and methods, it does it in real-time.
     
  38. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Okay, but that isn't going to solve my problem of this:

     
  39. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You really should have mentioned that at the beginning, because it changes everything :)

    Can you maybe explain what you're trying to do?
     
  40. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Yes, yes indeed! This does add a layer of complexity. For starters, you have to have both scenes loaded simultaneously - that means that when the second scene is loaded, you should be using one of the functions that looks like Application.LoadLevelAdditive("somelevel").

    Secondly, it's important to know which order these scenes are being loaded in. For this post, I'm going to assume that the script running the code is being loaded after the other one, because that's the simple way to do this. If the other one is being loaded first, that's fine, there's just more things you will need to do, so include that in your next comment.

    Finally, I'm going to make a recommendation that will go along with the examples and explanations we've been giving here, and that is GameObject.Find. However, it's important that you understand that GameObject.Find sucks, and you should make it a point to learn the better alternatives and not get in the habit of using it. I can explain more on that later. But basically, A) some of the better alternatives are more complicated, and B) exactly which alternative you want to use depends on what exactly your ultimate goal is.

    So, GameObject.Find gets you a reference to a GameObject in the scene by name. It doesn't matter whether the scene was loaded at the same time of beforehand or where it is, GameObject.Find will find it. You use it like this:
    Code (csharp):
    1. GameObject filingCabinet = GameObject.Find("object name");
    Once you have this GameObject in hand, that's your "filing cabinet", and you can proceed to use Kelso's code there:
    Code (csharp):
    1. int x = filingCabinet.GetComponent<FolderOne>().someValue;
    And that's it! ...as long as nothing goes wrong. (Which is what I mean when I say that GameObject.Find sucks - it's very easy to break it on accident, even if your code hasn't changed.)
     
  41. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    So, the problem I'm having is, it seems to load my 2nd scene an unlimited amount of time. How do I make it load the scene 1 time?

    press_start.cs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class press_start : MonoBehaviour {
    5.     //Sound
    6.     public AudioClip startup;
    7.     private AudioSource source;
    8.     public AudioClip titleScreenMusic;
    9.     private AudioSource source2;
    10.     //Other
    11.     public Sprite regular;
    12.     private SpriteRenderer sprite_renderer;
    13.     public int press_enter = 0;
    14.     //Time
    15.     public float timeRemaining = 5;
    16.  
    17.     //Gameobject thingy
    18.    
    19.  
    20.     public int house = 0;
    21.    
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.         //press start graphic
    27.         GetComponent<SpriteRenderer>().sprite = regular;
    28.         sprite_renderer = GetComponent<SpriteRenderer> ();
    29.         //sound (startup)
    30.         source = GetComponent<AudioSource>();
    31.         source2 = GetComponent<AudioSource>();
    32.         source2.PlayOneShot(titleScreenMusic, 1F);
    33.        
    34.     }
    35.    
    36.     // Update is called once per frame
    37.     void Update () {
    38.  
    39.         if (Input.GetKeyDown (KeyCode.Return) && press_enter == 0)
    40.         {
    41.             press_enter = 1;
    42.             sprite_renderer.enabled = false;
    43.             source.PlayOneShot(startup, 1F);
    44.         }
    45.  
    46.         if (press_enter == 1)
    47.         {
    48.             timeRemaining -= Time.deltaTime;
    49.             house = 1;
    50.            
    51.         }
    52.  
    53.         if (timeRemaining < 0)
    54.         {
    55.            
    56.             Application.LoadLevelAdditive("path");
    57.            
    58.         }
    59.  
    60.     }
    61.    
    62. }
    cutscene.cs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class cutscene : MonoBehaviour {
    4.     public Sprite home;
    5.     public Sprite inTheMailbox;
    6.     private SpriteRenderer sprite_renderer;
    7.    
    8.     public float timeRemaining2 = 4;
    9.  
    10.    
    11.  
    12.    
    13.  
    14.     public int house;
    15.  
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         GetComponent<SpriteRenderer>().sprite = home;
    21.  
    22.         sprite_renderer = GetComponent<SpriteRenderer>();
    23.  
    24.         GameObject filingCabinet = GameObject.Find("press enter");
    25.  
    26.         house = filingCabinet.GetComponent<press_start>().house;
    27.  
    28.  
    29.  
    30.  
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update () {
    35.  
    36.        
    37.  
    38.         //if (loadHouseLevel == 1)
    39.         //{
    40.         //    timeRemaining2 -= Time.deltaTime;
    41.         //}
    42.  
    43.  
    44.         if (timeRemaining2 > 0)
    45.         {
    46.             GetComponent<SpriteRenderer>().sprite = inTheMailbox;
    47.         }
    48.     }
    49. }
    50.  
     
  42. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    In your update, try switching your press_enter back to 0 when you load the next scene. Also, added in the <= sign, so if it hits 0 exactly, it'll still work.

    Code (CSharp):
    1. if (timeRemaining <= 0)
    2.         {
    3.             press_enter = 0;
    4.             Application.LoadLevelAdditive("path");        
    5.         }
    I could be wrong, but I think with LoadLevelAdditive, your script will stay active and keep reloading the new level over and over in update if you don't reset that press_enter flag.

    Edit: Err, I rather mixed that up, set your timer back away from 0. It's your timeRemaining that'll cause it to keep reloading once it hits 0 or less. So, instead of setting press_enter = 0, try setting timeRemaining = 5 again or whatever number greater than 0 you want. Or you could do the following:

    Code (CSharp):
    1. if(timeRemaining <= 0 && press_enter == 1)
    2. {
    3.     press_enter = 0;
    4.     Application.LoadLevelAdditive("path");
    5. }
     
  43. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    apsdsm likes this.
  44. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Thank you! I think that fixed all of my problems.
     
  45. 123fendas

    123fendas

    Joined:
    May 23, 2015
    Posts:
    44
    Well, my logic was/is, as long as I ask on the forums, I can make almost any game I want.
     
  46. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Well, no, that's that wrong approach. The correct approach is, As long as I learn how, I can make almost any game I want. This must start with tutorials. It's one thing to come here for help understanding a concept you've tried to read about. It's a very different thing to make this your only stop.

    Any one of the people replying here could have solved your problem 25 posts ago by simply handing you the right code. We didn't do that, because it's important to understand what you're doing. If we hand you the code instead of getting you to understand, that's not helping you - that's working for you. And you're not paying us to do that. If you want to pay us to do that, many of us will happily oblige, and we'll make your game for you, exactly as you want it. But if you're coming here to learn, meet us halfway, and do some tutorials first.
     
    _met44, Kiwasi, Lentaq and 4 others like this.
  47. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Does that apply if you have multiple sounds that need to be played from one object, each with different properties such as roll-off type, roll-off rate, max distance, etc. ? If so, could you provide a link or something for me to read?

    Or would the process be something along the lines of changing the audio source clip, and other properties via script before playing each sound? I imagine that would create issues with overlapping sounds though.
     
  48. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I wouldn't do it. It causes this behavior to be undefined
    Code (csharp):
    1.  
    2. GetComponent<AudioSource>().Play();
    3.  
    Which one is going to play? :)

    In the scenario you describe I would make the sounds child objects or set up a dedicated sound effect manager that keeps a pool of configured GOs with Audio Sources on them.
     
  49. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Several of us did hand over complete code. And it still didn't work. Hence my suggestion that game dev might not be the right hobby.
     
  50. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Oh no, I create the audio sources via script, and reference them with audio source variables.
    I understand what you meant now, there would be ambiguity using the line of code in your example.
     
    Last edited: Dec 2, 2015