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