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

Using " if " Statement from Different Scripts

Discussion in 'Scripting' started by gaishinbu, Oct 25, 2015.

  1. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Hi! Can I use and connect " if " statements from different scripts? For example;

    Code (CSharp):
    1.  
    2. //ScriptOne
    3.         if (hit.transform.gameObject.tag == "Enemy")
    4.         {            
    5.             Destroy(hit.transform.gameObject);
    6.         }
    7.      
    8. //ScriptTwo
    9.         if (thisList == enemylist)
    10.         {
    11.             Debug.Log("Enemy");
    12.         }
    13. //ScriptOne or ScriptTwo (and what I ask)
    14.         if (thisList == enemylist && hit.transform.gameObject.tag == "Enemy")
    15.        {
    16.             Destroy(hit.transform.gameObject);
    17.             Debug.Log("Enemy");  
    18.         }
    19.  


    Thank you very much for any help.
     
    AlmantusK likes this.
  2. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    Yes you can. You should store both ifs into a variable in each script locally and then access that variable through the thrid script using GetComponent<>().
     
    Last edited: Oct 25, 2015
  3. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    Inside first script:
    Code (CSharp):
    1. public bool condition1;
    2. //stuff to do
    3. condition1 = hit.transform.gemObject.tag == "Enemy";
    Inside second script:
    Code (CSharp):
    1. public bool condition2;
    2. //stuff to do
    3. condition2 = thisList == enemylist;
    combined:
    Code (CSharp):
    1. script1 s1 = gameObject.GetComponent<script1>();
    2. script2 s2 = gameObject.GetComponent<script2>();
    3. if(s1.condition1 && s2.condition2)
    4. //do your thing
    Keep in mind that you need public variables and public functions if you want to access them from another script.
     
    gaishinbu likes this.
  4. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Thank you very much!!! I will try!!!
     
  5. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    @AlmantusK Thank you very much for solution. It work perfect. But now, I have another problem :D How can I hide/show an object from list? Here;
    Code (CSharp):
    1.  
    2. ScriptOne s1 = gameObject.GetComponent<ScriptOne>();
    3.             if(s1.conditionList1 != null)
    4.             {
    5.                 if (hit.transform.gameObject.tag == "Apple" || hit.transform.gameObject.tag == "Car" || hit.transform.gameObject.tag == "Flower")  //Apple,car and flower just for example
    6.             {
    7.                     GameObject[] list1;
    8.                     list1 = GameObject.FindWithTag("Apple").SetActive(false); //ofcourse its not working. This line give me " CS0029: Cannot implicitly convert type `void' to `UnityEngine.GameObject[]' " error.
    9.                    
    10.          }
    11. }
    12.  
     
    Last edited: Oct 26, 2015
  6. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Code (CSharp):
    1. list1 = GameObject.FindWithTag("Apple");
    2. Foreach(GameObject oneApple in list1)
    3. {
    4. oneApple.SetActive(false);
    5. }
     
    gaishinbu likes this.
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    FindWithTag returns a single object...
     
    gaishinbu and CrymX like this.
  8. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    gaishinbu and Nigey like this.
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    that would stop the conversion error, yes :)
     
    gaishinbu likes this.
  10. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    @CrymX and @LeftyRighty thank you very much guys!! You are perfect! But :D I have another problem. I can't figure out. I try everythnk.

    Code (CSharp):
    1. ScriptOne s1 = gameObject.GetComponent<ScriptOne>();
    2.             if(s1.conditionList1) // This line give me this error: "NullReferenceException: Object reference not set to an instance of an object"
    3.             {
    4.                 if (hit.transform.gameObject.tag == "Apple")
    5.             {
    6.                     GameObject[] list1;
    7.                     list1 = GameObject.FindGameObjectsWithTag("Apple");
    8.                     foreach(GameObject oneApple in list1)
    9.                     {
    10.                         oneApple.SetActive(false);
    11.                     }
    12.                  
    13.                 gameController.AddScore (scoreValue);
    14.                 Destroy (hit.transform.gameObject);
    15.             }
    16.  
     
  11. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    you are welcome.

    i think the problem is : your gameObject don't have component "ScriptOne".

    so there's no reference to the propertie conditionList1 because s1 is null.
     
  12. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Thank you for fast reply and sorry for my inexperience. If its possible can you explane little more?Or with an example?
     
  13. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    your gameObject have some component (like a rigidbody, a script, a meshRenderer). If your gameObject don't have the component ScriptOne, GetComponent<ScriptOne> return null.
    So you can drag and drop your script into your GameObject or Add the componenent ScriptOne at runtime like this :


    Code (CSharp):
    1. void Start()
    2. {
    3. this.gameObject.AddComponent<ScriptOne>();
    4. }
     
  14. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. ScriptOne s1 = gameObject.GetComponent<ScriptOne>();
    3.  
    this requires a "ScriptOne" component to be on the same gameobject as this script... if there isn't one, s1 is null. You then try to access a property of s1 which causes the error.
     
  15. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Thank you guys @LeftyRighty and @CrymX but I used @CrymX 's code

    and now I get another error :/ I think I can't fix it.
    Code (CSharp):
    1.  
    2. //ScriptOne
    3. ...
    4. void UpdateScore ()
    5.     {
    6.         scoreText.text = "Score: " + score;  // this line. " NullReferenceException: Object reference not set to an instance of an object
    7.     }
    8. }
    9.  
    I hate " NullReferenceException " errors -_-
     
  16. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Can you show us the full code of ScriptOne ?
     
  17. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Of course;
    ScriptOne = GameController

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.EventSystems;
    5. using System.Linq;
    6.  
    7.  
    8. public class GameController : MonoBehaviour
    9. {
    10.  
    11.     public Camera cam;
    12.     public GameObject[] letters;
    13.     public GUIText scoreText;
    14.     private int score;
    15.     public GameObject splashScreen;
    16.     public GameObject startButton;
    17.  
    18.  
    19.  
    20.  
    21.     public GameObject[] list1;
    22.     public GameObject[] list2;
    23.     public GameObject[] list3;
    24.  
    25.  
    26.     private float maxWidth;
    27.  
    28.     void Start()
    29.     {
    30.         score = 0;
    31.         UpdateScore();
    32.  
    33.         if (cam == null)
    34.         {
    35.             cam = Camera.main;
    36.         }
    37.         Vector3 upperCorner = new Vector3(Screen.width, Screen.height, 0.0f);
    38.         Vector3 targetWidth = cam.ScreenToWorldPoint(upperCorner);
    39.         float letterWidth = letters[0].GetComponent<Renderer>().bounds.extents.x;
    40.         maxWidth = targetWidth.x - letterWidth;
    41.  
    42.     }
    43.  
    44.     public void StartGame()
    45.     {
    46.         splashScreen.SetActive(false);
    47.         startButton.SetActive(false);
    48.         StartCoroutine(Spawn());
    49.  
    50.         int rand = Random.Range(1, 3); //Note that the top end of Random.Range is non-inclusive
    51.  
    52.  
    53.         if (rand == 1) ShowOnScreen(list1);
    54.         if (rand == 2) ShowOnScreen(list2);
    55.         if (rand == 3) ShowOnScreen(list3);
    56.  
    57.     }
    58.  
    59.     public Vector3 startPoint = new Vector3(0, 0, 0);
    60.     public Vector3 endPoint = new Vector3(5, 0, 0);
    61.  
    62.     public bool conditionList1;
    63.     public bool conditionList2;
    64.     public bool conditionList3;
    65.     public void ShowOnScreen(GameObject[] thisList)
    66.      
    67.     {
    68.      
    69.         for (int i = 0; i < thisList.Length; i++) {
    70.             Instantiate (thisList[i], Vector3.Lerp (startPoint, endPoint, (float)i / (thisList.Length - 1)), Quaternion.identity);
    71.         }
    72.         if (thisList == list1)
    73.             conditionList1 = thisList == list1;
    74.         {
    75.             Debug.Log("list1");
    76.         }
    77.         if (thisList == list2)
    78.             conditionList2 = thisList == list2;
    79.         {
    80.             Debug.Log("list2");
    81.         }
    82.  
    83.  
    84.     }
    85.  
    86.     IEnumerator Spawn()
    87.     {
    88.         yield return new WaitForSeconds(0.0f);
    89.         while (true)
    90.         {
    91.             GameObject letter = letters[Random.Range(0, letters.Length)];
    92.             Vector3 spawnPosition = new Vector3(
    93.                 Random.Range(-maxWidth, maxWidth),
    94.                 transform.position.y,
    95.                 0.0f
    96.                 );
    97.             Quaternion spawnRotation = Quaternion.identity;
    98.             Instantiate(letter, spawnPosition, spawnRotation);
    99.             yield return new WaitForSeconds(Random.Range(0.0f, 1.0f));
    100.         }
    101.     }
    102.  
    103.     public void AddScore (int newScoreValue)
    104.     {
    105.         score += newScoreValue;
    106.         UpdateScore();
    107.     }
    108.  
    109.     void UpdateScore ()
    110.     {
    111.         scoreText.text = "Score: " + score;
    112.     }
    113. }
    114.  
     
  18. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    I suppose you have attached the component GUIText via the inspector. Are you sure the GUIText exist in your scene when updateScore is called ?
     
  19. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    gaishinbu likes this.
  20. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Thanks for suggestion @LeftyRighty :) @CrymX If I delete this;
    Code (CSharp):
    1.  
    2. void Start()
    3.     {
    4.         this.gameObject.AddComponent<GameController>();
    5.     }
    6.  
    and do this;

    Code (Csharp):
    1.  
    2. if (s1.conditionList1 != null)
    3.  
    My other scripts works fine and I don't get Null error but this line/code doesn't work;

    Code (CSharp):
    1.  
    2. GameController s1 = gameObject.GetComponent<GameController>();
    3.             if (s1.conditionList1 != null) // Alert(In Visual Studio): The result of the expression is always true since a value of type 'bool' is never equal to 'null' of type 'bool?'  Alert(In Unity): The result of comparing value type `GameController.conditionList1' with null is `true'
    4.  
    Its don't give me error but its not work. @CrymX and yes. GUIText attached to component via the inspector.
     
    Last edited: Oct 30, 2015
  21. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Any idea for this problem? Nobody?

    Code (CSharp):
    1.  
    2. GameController s1 = gameObject.GetComponent<GameController>();
    3.             if (s1.conditionList1 != null) // Alert(In Visual Studio): The result of the expression is always true since a value of type 'bool' is never equal to 'null' of type 'bool?'  Alert(In Unity): The result of comparing value type `GameController.conditionList1' with null is `true'
    4.