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

Im having problem with boolean

Discussion in 'Scripting' started by Dolke, Jun 20, 2018.

  1. Dolke

    Dolke

    Joined:
    Dec 16, 2016
    Posts:
    39
    Hello all :D

    I have one problem with boolean.

    I want boolean from script1 to use in script2,I watched 100 posts about that but it does not work for me.

    People do like :
    Code (CSharp):
    1. public class Script2: MonoBehaviour {
    2.  
    3. public Script1 sc;
    4.  
    5. void Update()
    6.  
    7. {
    8.  
    9. if(sc.booleanthatineed){
    10.  
    11. //something
    12.  
    13. }
    14.  
    15. }
    16.  
    17. }
    But I cant declare script1 in my other script (Script2 in this case).It is like script1 is not there.

    Maybe Im so dumb or something else is going on.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Well...I know this is an example that you are giving...but there is no issue with it. But you didn't really tell us what issue you are encountering. And you're not really having an issue with boolean. You're simply trying to access a value in one script from another script, which this question is asked all over the forum.

    Either way, you probably want to show some actual code of the two scripts and tell us what error message you are encountering for us to better help you.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is it on the same game object or a different game object (the two scripts)?

    In the example you posted, you would have to drag n drop the game object with Script1 into the variable slot on Script2 in the inspector.
     
  4. Dolke

    Dolke

    Joined:
    Dec 16, 2016
    Posts:
    39
    Script1 is on different gameobject.

    That is the problem,I can not drag gameobject because when I compile script,there is no field for gameObject there is error.



    It looks like my script "PapirPicker" does not exist at all. "PapirPicker" is script1
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Make sure the class name is spelled exactly correct. Also make sure the script file itself is exactly the same.
     
  6. Dolke

    Dolke

    Joined:
    Dec 16, 2016
    Posts:
    39
    I checked 3 times.Everything is ok.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Well, try restarting Unity. But, since you didn't show us any additional code, we can only guess. You could have PapirPicker under a namespace or something is misspelled.

    Depending on what is wrong, you can try right clicking on the PapirPicker and selecting the lightbulb to see if you get any helpful suggestions.
     
  8. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Does Script1 look like this? I also added a test to see if sc was set or not

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Script2: MonoBehaviour
    6. {
    7. public Script1 sc;
    8.  
    9.     void Start()
    10.     {
    11.         if(!sc)
    12.         {
    13.             Debug.Log("sc was not set - please set it via the inspector");
    14.         }
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if(sc.booleanthatineed)
    20.         {
    21.             //something
    22.             Debug.Log("The Bool is True");
    23.         }
    24.     }
    25. }
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Script1: MonoBehaviour
    6. {
    7.     public bool booleanthatineed;
    8. }
    and just like the other guys have said you may have something missspelled. make sure the class name and the file name are the same. "PapirPicker"
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Post the code for your PapirPicker class. I don't know what you mean by ' "PapirPicker" is script1 '.

    If "PapirPicker" is a GameObject name, that is NOT the name of a script and has nothing to do with it, unless it is coincidentally the same.
     
  10. Dolke

    Dolke

    Joined:
    Dec 16, 2016
    Posts:
    39
    Okey guys so I tried it in another script that I created,and it works.




    So what is the problem?

    Im trying to include this into Unity First Person Controller script.
     
  11. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    it must be a namespace in First Person Controller

    I think it's using this
    namespace UnityStandardAssets.Characters.FirstPerson
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think the issue could be that the standard assets are loaded before your own scripts.

    I'm not sure if there's a better way, but once I had the same issue, I copied the script so it existed outside of standard assets.