Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Alfisti · Jun 27, 2017 at 02:08 AM · c#variablehow-tochange variable

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alfisti · Jun 28, 2017 at 01:03 PM 0
Share

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

avatar image Nixmortem Alfisti · Jun 28, 2017 at 04:55 PM 0
Share

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?

avatar image Alfisti Nixmortem · Jun 28, 2017 at 07:03 PM 0
Share

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

}

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

376 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase value by one each second [C#] 3 Answers

cant access variable due to its protection 1 Answer

How do I compare if a variable of integer type is between two values 1 Answer

How to give Vector3 position to object being instantiated 1 Answer

Reading from file (C#) + Information from scene to scene (C#) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges