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

Hi, how can i send data to another class?

Discussion in 'Scripting' started by bali_, Dec 29, 2016.

  1. bali_

    bali_

    Joined:
    Oct 24, 2014
    Posts:
    9
    i got this class
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class FisierCod : MonoBehaviour {

    bool isRed;
    // Use this for initialization
    void Start () {

    isRed = false;

    }

    // Update is called once per frame
    void Update ()
    {

    if (Input.GetKeyDown (KeyCode.R))
    {
    if (isRed == false)
    {
    gameObject.GetComponent<Renderer> ().material.color = Color.red;
    isRed = true;
    }
    else
    {
    gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    isRed = false;
    }
    //Debug.Log (isRed);
    setRed(isRed);
    Debug.Log ("getRed" + getRed ());
    }

    }

    void setRed(bool red)
    {
    isRed = red;

    }
    public bool getRed()
    {
    return isRed;
    }

    }
    and i want to send red to another class here
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class SphereCode : MonoBehaviour {
    bool getRed;
    FisierCod newObject = new FisierCod();
    // Use this for initialization
    void Start () {
    getRed = false;

    }

    // Update is called once per frame
    void Update () {
    getRed = newObject.getRed();
    // Debug.Log ("get sphere " + getRed);
    if (getRed == true)
    { gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    }
    //Debug.Log (newObject.isRed);
    }
    }

    but it doesnt sends
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  3. bali_

    bali_

    Joined:
    Oct 24, 2014
    Posts:
    9
    how to fix the indentation ?
     
  4. bali_

    bali_

    Joined:
    Oct 24, 2014
    Posts:
    9
    public class FisierCod : MonoBehaviour {

    static bool isRed;
    void Start ()
    {
    isRed = false;
    }
    void Update ()
    {

    if (Input.GetKeyDown (KeyCode.R))
    {
    if (isRed == false)
    {
    gameObject.GetComponent<Renderer> ().material.color = Color.red;
    isRed = true;
    }
    else
    {
    gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    isRed = false;
    }

    setRed(isRed);

    }

    }

    void setRed(bool red)
    {
    isRed = red;

    }
    public bool getRed()
    {
    return isRed;
    }

    }

    this is first class here i set red to be true or false based on the R button i press from keyboard
     
  5. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FisierCod : MonoBehaviour {
    6.  
    7.   bool isRed;
    8.   // Use this for initialization
    9.   void Start () {
    10.  
    11.     isRed = false;
    12.  
    13.   }
    14.  
    15.   // Update is called once per frame
    16.   void Update ()
    17.   {
    18.  
    19.     if (Input.GetKeyDown (KeyCode.R))
    20.     {
    21.       if (isRed == false)
    22.       {
    23.         gameObject.GetComponent<Renderer> ().material.color = Color.red;
    24.         isRed = true;
    25.       }
    26.       else
    27.       {
    28.         gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    29.         isRed = false;
    30.       }
    31.       //Debug.Log (isRed);
    32.       setRed(isRed);
    33.       Debug.Log ("getRed" + getRed ());
    34.     }
    35.  
    36.   }
    37.  
    38.   void setRed(bool red)
    39.   {
    40.     isRed = red;
    41.  
    42.   }
    43.   public bool getRed()
    44.   {
    45.     return isRed;
    46.   }
    47.  
    48. }
    and i want to send red to another class here
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SphereCode : MonoBehaviour {
    6.   bool getRed;
    7.   FisierCod newObject = new FisierCod();
    8.   // Use this for initialization
    9.   void Start () {
    10.     getRed = false;
    11.  
    12.   }
    13.  
    14.   // Update is called once per frame
    15.   void Update () {
    16.     getRed = newObject.getRed();
    17.     // Debug.Log ("get sphere " + getRed);
    18.     if (getRed == true)
    19.     {
    20.       gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    21.     }
    22.     //Debug.Log (newObject.isRed);
    23. }
    24. }
    You are using the new() keyword on a monobehaviour, this is not how it works, because that instance is not connected to any gameobject (and thus will not get updated) instead use

    Code (csharp):
    1. FisierCod newObject = GetComponent<FisierCod>();
    And make sure a FisierCod component is attached to the same GameObject.

    Alternatively you could implement other ways to find the FisierCod component in your scene
    Code (csharp):
    1. public FishierCod newObject;
    Will allow you to drag the FisierCod in the inspector.
     
    Last edited: Dec 29, 2016
    bali_ likes this.
  6. bali_

    bali_

    Joined:
    Oct 24, 2014
    Posts:
    9
    ow man, its still without indentation
     
  7. bali_

    bali_

    Joined:
    Oct 24, 2014
    Posts:
    9
  8. Ashmouz

    Ashmouz

    Joined:
    May 22, 2015
    Posts:
    4
    You can grab the boolean from your first script just by adding public. I dont think you need all those lines.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FisierCod : MonoBehaviour {
    6.  
    7. public bool isRed;
    8. // Use this for initialization
    9. void Start () {
    10.  
    11. isRed = false;
    12.  
    13. }
    14.  
    15. // Update is called once per frame
    16. void Update ()
    17. {
    18.  
    19. if (Input.GetKeyDown (KeyCode.R))
    20. {
    21. if (isRed == false)
    22. {
    23. gameObject.GetComponent<Renderer> ().material.color = Color.red;
    24. isRed = true;
    25. }
    26. else
    27. {
    28. gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    29. isRed = false;
    30. }
    31. //Debug.Log (isRed);
    32. setRed(isRed);
    33. Debug.Log ("getRed" + getRed ());
    34. }
    35.  
    36. }
    and on the other script

    Code (CSharp):
    1. public class SphereCode : MonoBehaviour {
    2.  
    3. public FisierCod fisier;
    4.  
    5. // Use this for initialization
    6. void Start () {
    7.  
    8. // If the FisierCod Script is attached to the Sphere it self use this:
    9. fisier = gameObject.getcomponent<FisierCod>();
    10.  
    11. //If the FisierCod Script is attached on other GameObject then finethe Game Object by using its tag (Create its own tag if needed)
    12.  
    13. fisier = GameObject.FindGameObjectsWithTag("TheTag").getcomponent<FisierCod>();
    14.  
    15.  
    16. getRed = false;
    17.  
    18. }
    If you dont know how to tag:
    https://docs.unity3d.com/Manual/Tags.html

    I hope this will help :)
     
  9. bali_

    bali_

    Joined:
    Oct 24, 2014
    Posts:
    9
    with public doesnt work, and how do i set tag to first class?
     
  10. Ashmouz

    Ashmouz

    Joined:
    May 22, 2015
    Posts:
    4
    You dont tag the script you tag the gameojbect that the script is attached to. if you cant pass the boolean to the second script, try putting your FisierCod.cs into Assets/StandardAssets/ folder. And I suggest you write your two scripts again because you dont need all those lines. I could do that for you if I really understood what your up to. Give me more information, To what your FisierCod and SphereCode are attached to, Or maybe some screenshots.
     
  11. TopPlay

    TopPlay

    Joined:
    Jul 8, 2014
    Posts:
    15
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class: MonoBehaviour {
    5. public bool isRed;
    6.  
    7. public event Action<Color> OnColorChanged;
    8. // Use this for initialization
    9. void Start () {
    10. isRed = false;
    11. }
    12. // Update is called once per frame
    13. void Update ()
    14. {
    15. if (Input.GetKeyDown (KeyCode.R))
    16. {
    17. if (isRed == false)
    18. {
    19. gameObject.GetComponent<Renderer> ().material.color = Color.red;
    20. isRed = true;
    21. }
    22. else
    23. {
    24. gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    25. isRed = false;
    26. }
    27. if(OnColorChanged!=null)
    28. OnColorChanged.invork(Color.blue);
    29. //Debug.Log (isRed);
    30. setRed(isRed);
    31. Debug.Log ("getRed" + getRed ());
    32. }
    33. }
    and you can Registrer event for this calss like this:
    Code (CSharp):
    1. _FisierCod=transform.getcompent<FisierCod>();
    2. if (_FisierCod!=null)
    3. _FisierCod.OnColorChanged+=YourColorChangedFunc;
     
  12. bali_

    bali_

    Joined:
    Oct 24, 2014
    Posts:
    9
    i want to change color when i press R on a cube after that in second class i want to verify if the color of the cube is changed than i can change the color of the second cube