Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
-1
Question by hmlanila · Jul 30, 2013 at 11:23 AM · scripting problemyieldenter-car

Can somebody see the error(s) here ? C# Scripting

Hey, i have this script. It is about entering and exiting cars. I have many cars in the project, every car has this script attached, and configured. But i enter this car, then i exit it, everything works fine, but after i drive, and enter/exit few cars, script gets crazy - as i - enter one car, when i pres the 'exit key', instead of exiting a car, i end up driving another, then press again exit - i end up driving two cars in the same time....weird. This is the script:

 using UnityEngine;
 using System.Collections;
 
 public class EnterExit : MonoBehaviour {
     
     
     public Drivetrain Drivetrain;
     public CarController CarController;
     public SoundController SoundController;
     public  GameObject Car;
     public  GameObject ExitPoint;
     public  GameObject CarCamera;
     public  GameObject Player ;
     public  GameObject PlayerCamera;
 
 
     bool IsPlayerInCar = false;
     bool IsPlayerInTrigger = false;
     // Use this for initialization
 void Start () {
     CarController  = GetComponent (typeof (CarController)) as CarController;
     SoundController  = GetComponent (typeof (SoundController)) as SoundController;
 
     }
 
     
 void OnTriggerEnter(Collider Player) {
         
     IsPlayerInTrigger = true;
     }
 void OnTriggerExit(Collider Player) {
     
     IsPlayerInTrigger = false;
     }
 
     // Update is called once per frame
     void Update () {
 
     if (Input.GetKeyDown(KeyCode.F)) {
         if (!IsPlayerInCar && !IsPlayerInTrigger) 
             { 
             // Actions to perform when not in car, and not in trigger
             }
         if (!IsPlayerInCar && IsPlayerInTrigger) 
             { 
                 // Actions to perform when not in car but in car's trigger
                 CarController.enabled = true;
                 Drivetrain.enabled = true;
                 CarCamera.camera.enabled = true;
                    CarCamera.gameObject.GetComponent<AudioListener> ().enabled = true;
                 PlayerCamera.camera.enabled = false;
                 PlayerCamera.gameObject.GetComponent<AudioListener> ().enabled = false;
                 Player.gameObject.transform.parent = ExitPoint.transform;
                 Player.transform.localPosition = new Vector3(0,0,0);
                 Player.transform.localRotation = new Quaternion(0,0,0,0);
                 Player.gameObject.SetActive(false);
                 StartCoroutine(EnterCar());
     }
             
         if (IsPlayerInCar) 
             { 
                 // Actions to perform when in car (to exit car)
                 CarController.brake = 999;
                 CarController.enabled = false;
                 Drivetrain.enabled = false;
                 CarCamera.camera.enabled = false;
                    CarCamera.gameObject.GetComponent<AudioListener> ().enabled = false;
                 PlayerCamera.camera.enabled = true;
                 PlayerCamera.gameObject.GetComponent<AudioListener> ().enabled = true;
                 Player.gameObject.transform.parent = null;
                 Player.transform.localRotation = new Quaternion(0,0,0,0);
                 Player.gameObject.SetActive(true);
 
                 StartCoroutine(ExitCar());
                              
             }
         }
     }
 IEnumerator EnterCar()
     {
     yield return new WaitForSeconds(0.5f);
     IsPlayerInCar = true;
     SoundController.EngineOn = true;
     }
 IEnumerator ExitCar()
     {
     yield return new WaitForSeconds(1);
     IsPlayerInCar = false;
     SoundController.EngineOn = false;
 
     }
 }
Comment
Add comment · Show 3
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 amphoterik · Jul 30, 2013 at 11:54 AM 0
Share

I haven't read through the entire script, but I noticed you named it EnterExit. Generally speaking, na$$anonymous$$g (and further modeling) a class after an action is a bad idea and can lead to organizational and conceptual issues. Why can't the enter and exit methods be a part of the greater car class? It stands to reason that some of your issues comes from the way you are separating the concerns of the car functionality.

avatar image Ulthias · Jul 30, 2013 at 12:18 PM 0
Share

For what its worth I would say that na$$anonymous$$g a class as a behaviour/action is not only recommended but also reflected by Unity's model seeing as the classes are inherited from a Behaviour-based class ($$anonymous$$onoBehaviour). A $$anonymous$$onoBehaviour class, the way I see it, is a way for a Unity user to define specialized behaviours for each of their game objects. Beyond that, I would say the problem is most likely do with the way you're using coroutines in the Update() method. I'd think that because you're not updating the behaviour state for X second (and Update() is still called for a few times) you have more coroutines created on the object; I think anyway, not having run the code myself. $$anonymous$$y suggestion, create a 'UseCar' behaviour and attach it to the player which is triggered when the right requirements are met (player is next to a car and player pressed correct input).

avatar image hmlanila · Jul 30, 2013 at 04:31 PM 0
Share

I am quite new to scripting, my first if(){} was written about 3 months ago, and till than i had no contact with any scripting language. So i dont quite understand how behaviours should and be used. Do you have any tutorials to guide me to ? Thanks!

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

How can I make the health points constantly go down? 1 Answer

Best way to emulate Swift String Interpolation in UnityScript? 1 Answer

Is there anybody who can look into my code to help me in resolving my error? Data in Database is not updating. 0 Answers

Access unity Image.Alpha? Assets/Scripts/GUI/Missionupdate.cs(17,23): error CS1061: Type `UnityEngine.UI.Image' does not contain a definition for `Alpha' and no extension method `Alpha' of type `UnityEngine.UI.Image' 2 Answers

How to simply enable an objects particle system emission when that object collides with another? 1 Answer


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