Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by FinKone · Jun 27, 2013 at 10:51 PM · transformplayer

Writing my own enter/exit car - need help.

Ok guys so I'm writing my own script for once - its a enter and exit car deal.

Everything is working fine to the point I got to - but I'm about to take a break was wondering if someone would throw me a bone on the best way to attack my next goal....

I currently have it so when my player enters the car, disables the player object, renderer.enabled = false kicks off for the player, disables the player camera and enables the car camera - I don't have any car controls implemented yet... I already have a way to trigger that - the only thing really missing before I move on to play with car controls is making sure I get my player transforms right... I was wondering what would be a good method for transforming my player to the car on the keydown press to get out at new location.

I also seem to have a slight problem finding the right syntax for disabling JUST a script in a child of a object using C#...

I can post code if you want - this is more of a theory talk I guess? -.-

My problem lately it seems in my learning is turning my ideas into code from a logical thought process - I completely fail at applying logic written steps to accomplish my goals and often get lost in the sauce from jumping right in...

So if anyone has anything on object orientated programming that would help me organize myself before starting to code that would be great also...

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
0
Best Answer

Answer by PangeaMMO · Jun 28, 2013 at 05:12 AM

To handle the player getting out i would calculate everything relative to the car object. I would start with the player inside the car, play the animation of getting out of the car, then enable the character controller / player physics.

To disable a script you could try...

 GameObject.GetComponent<scriptName>().enabled = false;

i am not certain if that would work, you could also put a method in your script an call it externally. The method would look like this.

 public class MyScript() : MonoBehaviour {
 
   private bool scriptEnabled = true;
 
   void Start() {
     if (scriptEnabled) {
     // your script here
     }
   }
 
   void update() {
     if (scriptEnabled) {
     // more of your script here
     }
   }
 
   public void DisableScript() {
     scriptEnabled = false;
   }
 
 
 

To call this method from another script you would use...

 GameObject.GetComponent<myScript>().DisableScript();
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
avatar image
0

Answer by FinKone · Jun 28, 2013 at 01:40 PM

Well in regards to accessing and disabling another script from a different one, the syntax got really retarded fast. This his how I was able to turn a script off...

         //assigne game object PlayerCar, Component Car Controller to carController script...
         carController = GameObject.Find ("PlayerCar").GetComponent("CarController") as CarController;
         //disable the script.
         carController.enabled = false;

I had to look into a lot of examples, and I don't entirely understand the first line of code, I do however understand it works for its needs.

Comment
Add comment · Show 2 · 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 PangeaMMO · Jun 28, 2013 at 02:44 PM 0
Share
 GameObject.Find ("PlayerCar")

This finds the PlayerCar object. Isn't this script attached to PlayerCar? If it is you can simplify it to...

 GameObject.GetComponent<CarController>()


 GetComponent("CarController") as CarController

This piece of code may work, but it isn't quite perfect syntax. GetComponent for C# is different than unity script.

 GetComponent("CarController") as CarController
 
 should be
 
 GetComponent<CarController>() as GameObject

The reason for putting the scripts name in <> is because your trying to find the component based on its name, which is a string. C# will complain if you put a string in the (). The only time you will use the () is if you need to access an component that you know the type of, such as a collider would look like

 GetComponent(Collider) as GameObject

Collider is a type, not a string.

Unless you are using the carController variable for something else in the script you really dont even need it. You could just do

 GameObject.GetComponent<CarController>().enabled = false;


 
 
avatar image FinKone · Jun 28, 2013 at 06:04 PM 0
Share

This doesn't work for me - sorry. I understand how to link things from one script to another just fine. The Enter/Exit script is a child of the Car Object that its attached to to keep things organized. When I try the above method - which I've located in other posts (that also didn't work for those people) it just doesn't work. Things such as "enabled" / no extension method "enabled" for that type start to pop up. I know I'm ref'in the scripts right.

So again thanks for trying to make something work better - but this isn't a solution to a problem I've already got solved... I know its bulky what I'm doing but I can refine it later.

The only way I've been able to get it to work is to assign it, then access it afterwards. Ever time I try the .enabled = false; way on the end of how you'd think you'd need to call it, it doesn't work...

Believe me the first thing I tried to accomplish this small goal was what you've typed out. I just didn't understand the code as reading it...

 carController = GameObject.Find ("CarV2").GetComponent("CarController") as CarController;
 
 carController is equal to a Game object I'm finding, called CarV2, within that I am getting a component (script) called CarController, and here is the only part I'm not getting that I was asking about - as CarController. 

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

16 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

Related Questions

Dynamic jump? 1 Answer

Need enemy to follow only active character 0 Answers

I want to find a transform when an object is instantiated 2 Answers

Nested Prefab Issue 0 Answers

UnityScript error in transform.Position 2 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