I am having a problem with a small project I am working on, based on a tutorial that I am working from, and hoping that someone may be able to help me out.
The problem is this: I need to stop a script controlling the car and another script that controls the Lap Timer to stop from running until the start counter has completed its operation and then start these scripts running after that to get control of the car and well, get that timer running :).
The script for the control of the car is based on the GameObject of the same name, and the script for the lap timer is in the GameManager Object. The way I have been working the project is to have all the different âmanagersâ in the one GameObject, so, this script which needs to restart these script is also in the GameManager object. The way the tutorial worked it is very confusing and used JavaScript of all things to reactivate the CarControls and that was just one step too far for me.
So, how to I go about stopping/restarting those scripts using C#?
To anyone who can help here is a big thankyou in advance!
âtheCarâ is a variable of type GameObject.
You shouldnât even be able to assign a component to it, which GetComponent does normally return.
Instead, youâd use a compatible variable type.
In order to be more type-safe and avoid error-prone strings in your code, youâd usually use the generic version of GetComponent in a fashion like
Thankyou for taking the time to respond. However I am not completely following what you are saying.
From what I understand about Unity, a connection must be made to the parent Object ie Car, otherwise, I wont know what object I am looking for. So, to achieve that I am doing: public GameObject theCar and drop the Car object into the inspector. If that is wrong please please tell me how i make a connection to the âCarâ GameObject?
Now, I need to access the script and turn it on/off. I donât understand your example code and what you are trying to explain with:
as this would result in an error. As there is no controller type.
Sounds like you need to step back from this problem for a second and get a grip on the basics. The learn section has some good videos that cover GetComponent. And the manual has some details on the relationship between GameObjects and Components.
Yeah, I could do with a good refresher course on components had a bugger of a time getting AudioSource and AudioClips to work in my current project ended up needing an AudioSource to play the AudioClips - completely wracked the olâ noggin.
However in this case a refresher course would not helped the scripts on the car object - the standard assest in the vehicle portion do not appear to be accessible through a component. I tried this out by adding another script to the Car Object and it showed up immediately.
So, it looks like Iâm screwed! And no amount of help, wouldâve worked, well not at my level of inexpertise.
Iâm looking into that right now, just started searching on it - but, at the moment it just like whoosh right over my head. I also have a martial arts lesson on tonight and Iâm worried about some of the students, so that is also playing on my mind. Itâs that time of week again where I start switching to martial arts mode and a lot less on everything else
[edit] Oh damn, it worked! A couple of errors, but, Iâll sort them out in due course, but, IT WORKED!!! And get the project sorted out correctly this is just the testing phase. But damn I thought this project was DOA.
I donât really understand how this directive works or why itâs needed but ⌠it works :):p:sunglasses:.
Problem solved!!! I canât thankyou enough, I just thought that was some error thrown up by the engine telling me I was screwed. Thatâll learn me to pay closer attention to the error messages.
My apologies, I accidently used âCarControllerâ since you were speaking about one, I should have taken the typeâs name (of the component that you were trying to get) from the script.
So the same code with âCarUserControlâ should have worked, given that you have included the correct namespaces.
Basically the syntax is still correct, as youâve found out by yourself later.