- Home /
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...
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();
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.
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;
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
![](https://koobas.hobune.stream/wayback/20220613105018im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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