Change a variable in another script when collider on trigger enter c#
Good night everybody, iam working on my car game and have made a fuel system. The consumption of fuel from car work fine, but I have troubles with my script to refuel.
Everytime that the car pass into collider trigger, i want add 5 liters of fuel, but I don't have sucessfull on this. My current script to refuel find with sucessfull who is the current player, the car script and the current fuel of the player car, all work fine, less the refuel function. All the things that i have made to try add new liters of fuel in the car script don't work.
If someone could me help me, thanks a lot, I don't know how add more liters with refuel script into the current fuel from the car controller script, thanks everybody!
using UnityEngine; using System.Collections; using UnityStandardAssets.Vehicles.Car;
public class FuelOnCollider : MonoBehaviour { public GameObject CurrentCar;
public CarController CurrentScript;
public float CurrentCarFuel;
public Light Light1;
public Light Light2;
void Start()
{
CurrentCar = GameObject.FindWithTag("Player");
CurrentScript = CurrentCar.GetComponent<CarController>();
Light1.enabled = false;
Light2.enabled = false;
}
void Update()
{
CurrentCar = GameObject.FindWithTag("Player");
CurrentScript = CurrentCar.GetComponent<CarController>();
CurrentCarFuel = CurrentScript.currentFuel;
}
void OnTriggerEnter(Collider other) {
{
if(CurrentCar.tag == "Player")
{
Light1.enabled = true;
Light2.enabled = true;
}
}
}
void OnTriggerExit(Collider other) {
{
if(CurrentCar.tag == "Player")
{
Light1.enabled = false;
Light2.enabled = false;
}
}
}
}
Answer by Nixmortem · Jun 28, 2017 at 03:43 AM
One thing you could do is have your fuel variable on your carController script and then on whatever script you have attached to the refueling pickup activate a refueling function. Like this:
Public carController carScript;
Public float refuel = 10;
Void Start (){
}
Void Update (){
}
Void OnColliderEnter (Collider other){
CarScript.fuel += refuel;
}
Something similar to that. You'd have to plug in all your tags and stuff though.
Hi @Nixmortem thanks a lot for the answer :D . I have try made the functions that you tell me but I have a trouble, when the car enter in collider, the fuel from car don't change, the fuel keep the same in the variable "liters" in CarController. Theres not error in console.
The current script based in your tips:
using UnityEngine; using System.Collections; using UnityStandardAssets.Vehicles.Car; using System.Text; using System.IO;
public class NewFuel : $$anonymous$$onoBehaviour {
public CarController carScript;
public float refuel = 10;
void Start (){
}
void Update (){
}
void OnColliderEnter (Collider other){
carScript.Liters += refuel;
}
}
If you could help me again thank you ! :D
Sorry I'm still pretty new. it should have been OnTriggerEnter in my first answer ins$$anonymous$$d of OnColliderEnter. Did you make sure to have the isTrigger box on your collider component set to true?
Hi bro, not problem I'm too dont know the scripts languages, thats a hobbie not my work. How my game is growing is a miracle haha. Yes the trigger box is true, now I have a funny thing to tell you, I put the same variables into my first script, and this work "more or less" haha.
The strange is that my car that before in game have 68 liters of fuel, when the game is play get new 100 liters LOL, making 168 liters :O haha . The good news, when the car enter in collider he refuel more 10 liter :D . Thanks a lot bro, I will try fix this. If you have interest in car games we can talk about, see you soon, thanks! :D.
using UnityEngine; using System.Collections; using UnityStandardAssets.Vehicles.Car; using System.Text; using System.IO;
public class FuelOnCollider : $$anonymous$$onoBehaviour { public GameObject CurrentCar;
public CarController CurrentScript;
public float CurrentCarFuel;
public Light Light1;
public Light Light2;
public float refuel = 10;
private GUIStyle guiStyle = new GUIStyle();
private int fontSize = 39;
private bool TextOn = false;
void OnGUI ()
{
if (TextOn == true)
{
guiStyle.fontSize = $$anonymous$$athf.$$anonymous$$in($$anonymous$$athf.FloorToInt(Screen.width * fontSize/1000), $$anonymous$$athf.FloorToInt(Screen.height * fontSize/1000));
guiStyle.normal.textColor = Color.green;
GUI.Label (new Rect( Screen.width * 0.355f , Screen.height * 0.45f , Screen.width * 0.4f , Screen.height * 0.25f),"Vehicle refueled: more 10 liters!", guiStyle);
GUI.enabled = true;
}
if (TextOn == false)
GUI.enabled = false;
}
void Start()
{
CurrentCar = GameObject.FindWithTag("Player");
CurrentScript = CurrentCar.GetComponent<CarController>();
Light1.enabled = false;
Light2.enabled = false;
}
void Update()
{
CurrentCar = GameObject.FindWithTag("Player");
CurrentScript = CurrentCar.GetComponent<CarController>();
CurrentCarFuel = CurrentScript.currentFuel;
}
void OnTriggerEnter(Collider other) {
{
if(CurrentCar.tag == "Player")
{
Light1.enabled = true;
Light2.enabled = true;
CurrentScript.Liters += refuel;
}
}
}
void OnTriggerExit(Collider other) {
{
if(CurrentCar.tag == "Player")
{
Light1.enabled = false;
Light2.enabled = false;
TextOn = false;
}
}
}
}
Answer by Alfisti · Jun 27, 2017 at 04:35 PM
Hi everybody, I have try made this variables in script.
public float Refuel = 10; (the variable of refuel that sould be add in the current fuel variable from CarControler script).
private int NewCurrentFuel; (variable that could be replace the car current fuel variable from CarControler script).
On Trigger Enter i made :
void OnTriggerEnter(Collider other) {
{
if(CurrentCar.tag == "Player")
{
Light1.enabled = true;
Light2.enabled = true;
NewCurrentFuel = CurrentCarFuel+Refuel;
}
}
When script is running the Unity show this error "Assets/System/Utility/Scripts/FuelOnCollider.cs(64,21): error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)"
My current script is :
using UnityEngine; using System.Collections; using UnityStandardAssets.Vehicles.Car; using System.Text; using System.IO;
public class FuelOnCollider : MonoBehaviour { public GameObject CurrentCar;
public CarController CurrentScript;
public float CurrentCarFuel;
public Light Light1;
public float Refuel = 10;
public Light Light2;
private GUIStyle guiStyle = new GUIStyle();
private int fontSize = 39;
private int NewCurrentFuel;
private bool TextOn = false;
void OnGUI ()
{
if (TextOn == true)
{
guiStyle.fontSize = Mathf.Min(Mathf.FloorToInt(Screen.width * fontSize/1000), Mathf.FloorToInt(Screen.height * fontSize/1000));
guiStyle.normal.textColor = Color.green;
GUI.Label (new Rect( Screen.width * 0.355f , Screen.height * 0.45f , Screen.width * 0.4f , Screen.height * 0.25f),"Vehicle refueled: more 10 liters!", guiStyle);
GUI.enabled = true;
}
if (TextOn == false)
GUI.enabled = false;
}
void Start()
{
CurrentCar = GameObject.FindWithTag("Player");
CurrentScript = CurrentCar.GetComponent<CarController>();
Light1.enabled = false;
Light2.enabled = false;
}
void Update()
{
CurrentCar = GameObject.FindWithTag("Player");
CurrentScript = CurrentCar.GetComponent<CarController>();
CurrentCarFuel = CurrentScript.currentFuel;
}
void OnTriggerEnter(Collider other) {
{
if(CurrentCar.tag == "Player")
{
Light1.enabled = true;
Light2.enabled = true;
NewCurrentFuel = CurrentCarFuel+Refuel;
}
}
}
void OnTriggerExit(Collider other) {
{
if(CurrentCar.tag == "Player")
{
Light1.enabled = false;
Light2.enabled = false;
TextOn = false;
}
}
}
}
If someone could help me, thanks a lot!
Your answer
![](https://koobas.hobune.stream/wayback/20220612124951im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate a script into an instantiated prefab 3 Answers
How to change the player gravity level?,How to change the value of the players gravity? 1 Answer
C# float variable isnt behaving like its value 1 Answer
Changing an argument to a var and vice-versa 0 Answers
How do I save custom variables? 1 Answer