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