[Solved] How to Stop/Restart a Script?

Hello Guys,

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!

Regards.

A bit confusing to read but check this out.

Also OnEnable and OnDisable can we worth looking up!

1 Like

Thanks greatly for your reply.

Right, I tried accessing the CarUserControl script like this using in the vars section:

public GameObject theCar;

then using this code in Start to test it:

theCar = theCar.GetComponent("CarUserControl");
theCar.enabled = true;

and received this error:

So would you be able to tell me what I am doing wrong here? I can’t figure this out!?!

‘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

CarController controller = theCar.GetComponent<CarController>();

Make sure you do a null-check before you try to work with the returned result.

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.

So, I am a little lost and need your advice.

I have found some example code that apparently explains what I am supposed to do to get it working, thusly:

  • GameObject varGameObject = GameObject.Find(“object”); or find with tag

  • varGameObject.GetComponent().enabled = true;

But I cannot understand how I am suppossed to write in the Script name. If I put the CarUserControl script with quotes I get an error:

and if I type in the name CarUserControl without quotes I get another error:

I am sure I am doing some simple thing wrong but cannot figure out what?

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.

2 Likes

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.

That was my suggestion, increase your level of expertise.

Did you try adding the using statement as suggested by the error?

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 :frowning:

Its just asking you to type this at the top of your script.

using UnityStandardAssets.Vehicles.Car;
1 Like

Oh, riiiiight, so, that’s what a Directive is!

[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.

Thankyou, thankyou, thankyou.

Beir bua agus beannacht!!!

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.

1 Like