The question is answered, right answer was accepted
How can i change bool of other gameobject?
Hi there, i have a first person character and when i press "E" key near a gameobject (boat) the character go in this gameobject. Now i want to move this boat and i have 3 bool created: CanControll, RotateLeft and RotateRight.
I want to change this bool from character controller script but i get error everytime.
Character controller script when i press E key:
if (inputAccion) {
//Debug.Log ("E Press");
bool CloSeToBoat = CheckCloseToTag("Boat", 3);
if(CloSeToBoat){
//Debug.Log("boat is near");
if(!OnBoat){
GameObject[] Boat;
Boat = GameObject.FindGameObjectsWithTag("Boat");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in Boat) {
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
GameObject MyBoat = closest;
gameObject.transform.parent = MyBoat.transform;
gameObject.transform.localPosition = new Vector3(0, 15, -12f);
GameObject camera = GameObject.Find ("Main Camera");
camera.transform.localPosition = new Vector3(0, 15, -62f);
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
camera.transform.localRotation = Quaternion.Euler(0, 0, 0);
gameObject.GetComponent<MouseLook>().CanLook = false;
GameObject father = gameObject.transform.parent.parent.gameObject;
father.GetComponent<BoatController>().CanControll = true;
Debug.Log("Name: "+father);
OnBoat = true;
canControl = false;
}
I get error in this line:
father.GetComponent().CanControll = true;
Here the Boat Script:
using UnityEngine;
using System.Collections;
public class BoatController : MonoBehaviour {
public float BoatSpeed = 2;
public float RotateSpeed = 15;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public bool RotateLeft = false;
public bool RotateRight = false;
public bool CanControll = false;
private Vector3 moveDirection = Vector3.zero;
void Start(){
CanControll = false;
}
void Update() {
if (CanControll) {
Debug.Log ("speed:"+ BoatSpeed);
transform.Translate(Vector3.forward * (Time.deltaTime * BoatSpeed));
if (RotateLeft) {
transform.Rotate(Vector3.up * (Time.deltaTime * RotateSpeed));
}
if (RotateRight) {
transform.Rotate(Vector3.down * (Time.deltaTime * RotateSpeed));
}
}
}
}
Please help me. Thak you
Answer by Denvery · Jan 04, 2016 at 07:50 PM
As I can see this expression father.GetComponent() returns null.
It means that the "father" object does not contain script "BoatController".
Are you sure that line
GameObject father = gameObject.transform.parent.parent.gameObject;
returns correct object?
You can check it in logs by writing line like this: Debug.Log("father name = " + father.name)
The log say me this: father name = BoatGal
BoatGal is the name of the gameobject that contains BoatController script.
Please write to log this line:
Debug.Log("boat = " + father.GetComponent<BoatController>());
We need to check if it is == null
I cant execute it because i get this error:
error CS0246: The type or namespace name 'BoatController' could not be found. Are you missing a using directive or an assembly reference?
@Denvery Now im trying to get the script of the gameobject like this:
GameObject BoatTo$$anonymous$$ove = GameObject.Find("BOATGal1");
Debug.Log ("CanControll : "+BoatTo$$anonymous$$ove.GetComponent<BoatController>().CanControll);
Im geting the same error and i dont know why. The gameobject with name "BOATGal1" have a script into it names "BoatController":
I get this error in my console and i cant execute the game:
Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.cs(438,78): error CS0246: The type or namespace name `BoatController' could not be found. Are you missing a using directive or an assembly reference?
Please help me. Thank you
CharacterController can't see "BoatController". Try reimport BoatController script.
I solve it. I have not the "BoatController" script in the same directory of "Character$$anonymous$$otor" script. I move it and no it works.
But no i cant get the bool from BoatController script. Im using this script to get the bool:
Debug.Log ("Name : "+BoatTo$$anonymous$$ove.GetComponent().CanControll);
The console send me this error: NullReferenceException: Object reference not set to an instance of an object Character$$anonymous$$otor.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.cs:442)