Hi, how can i send data to another class?

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 ().material.color = Color.red;
isRed = true;
}
else
{
gameObject.GetComponent ().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 ().material.color = Color.blue;
}
//Debug.Log (newObject.isRed);
}
}

but it doesnt sends

I don’t understand your code because of the missing indentation, please fix it
Another question, how much C# did you learn before attemting unity?

how to fix the indentation ?

public class FisierCod : MonoBehaviour {

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

if (Input.GetKeyDown (KeyCode.R))
{
if (isRed == false)
{
gameObject.GetComponent ().material.color = Color.red;
isRed = true;
}
else
{
gameObject.GetComponent ().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

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);
}
}

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

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

public FishierCod newObject;

Will allow you to drag the FisierCod in the inspector.

1 Like

ow man, its still without indentation

thank you @dterbeest

You can grab the boolean from your first script just by adding public. I dont think you need all those lines.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FisierCod : MonoBehaviour {

public 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 ());
}

}

and on the other script

public class SphereCode : MonoBehaviour {

public FisierCod fisier;

// Use this for initialization
void Start () {

// If the FisierCod Script is attached to the Sphere it self use this:
fisier = gameObject.getcomponent<FisierCod>();

//If the FisierCod Script is attached on other GameObject then finethe Game Object by using its tag (Create its own tag if needed)

fisier = GameObject.FindGameObjectsWithTag("TheTag").getcomponent<FisierCod>();


getRed = false;

}

If you dont know how to tag:

I hope this will help :slight_smile:

with public doesnt work, and how do i set tag to first class?

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class: MonoBehaviour {
public bool isRed;

public event Action<Color> OnColorChanged;
// 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;
}
if(OnColorChanged!=null)
OnColorChanged.invork(Color.blue);
//Debug.Log (isRed);
setRed(isRed);
Debug.Log ("getRed" + getRed ());
}
}

and you can Registrer event for this calss like this:

_FisierCod=transform.getcompent<FisierCod>();
if (_FisierCod!=null)
_FisierCod.OnColorChanged+=YourColorChangedFunc;

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