Can't disable component (script)

I’m making a short transition into the start of the game, and I want to disable the script controlling the player and camera movement.

Here’s what I got so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class GameStart : MonoBehaviour {

    public Camera cam;
    public AudioSource audioSource;
    public AudioClip turnOn;
    public AudioClip beep;
    FirstPersonController player;

    public IEnumerator Start () {
        player.enabled = false;
        audioSource.PlayOneShot (beep);
        yield return new WaitForSeconds (1);
        audioSource.PlayOneShot (turnOn);
        Destroy (cam);
        player.enabled = true;
    }

Basically I have a plane with a black material in front of the player, which is a child of the camera I’m disabling.

I have everything setup correctly (all the correct objects put into the inspector fields).

As soon as the game starts, it’s supposed to disable the movement script, play a sound, wait one second, play another sound, remove the camera along with its child so the player can see, and enable the script so they can move.

I tried first calling a GameObject for the player, then doing “GetComponent” to get the script, but then the “enabled” both turn red. And I don’t think I need to specifically state the script as a component for this to work, as technically it should just disable any and all instances of that script.

I’m currently not getting any errors or anything like that, but it just doesn’t seem to be working. If I remove the “enabled” lines, the sounds play and the camera is removed. So it’s definitely the enabling and disabling that’s the problem.

Also, I don’t know if just removing the camera, removes the children. So what I got currently, is a “Black Screen” layer, which only that camera can see. So if I remove the camera, the plane isn’t being rendered. But I just wanna make sure it’s removed, just incase it causes some raycast-type malfunctions in the future or something.

This script (GameStart) isn’t on your player object, right? (Or child of it).

And I wasn’t too clear what happens with the enabled lines in there… none of that code in GameStart Start() triggers and the player can move?

The script is on the main camera, which is a child of the player.

And yes, I just added debug lines, and none of the code in “Start” is triggering.

I also just noticed an error popping up in the console:
3093274--233383--upload_2017-6-3_0-31-59.png

Oh and I know you said you have inspector stuff hooked up correctly, but I’ll ask anyways… the object with FirstPersonController on it is dragged into inspector (presumably your player)? I ask, because it looks like it’s erroring that it’s not set up, if that error line number matches your code above.

1 Like

Oh and your last post tells me the problem… your disabling the parent of the script you’re executing… that disables the script … that’s the problem, I believe. Can you move it elsewhere? An empty object… not a child of anything

I didn’t set the script reference as public, so I couldn’t drag anything in. I thought since I was making a direct reference to the script, I didn’t need to drag in an object (even though I did in another script). :sweat_smile:

Yeah you definitely need to use getcomponent for that or declare public and use inspector. But it also sounds like you’re going to disable this script mid execution with how you have it. Sorry, on mobile … hopefully I’m making sense here lol

Well no, I’m not disabling this script. I’m disabling the script FirstPersonController script.

On my laptop now, so I can actually communicate a bit better and visualize all this a little better. Anyways, my mistake on that, I think I’m following along here better, and it just seems like all you need to do is assign that player variable to something. Have you dont that yet? Either make it public, and drag your player on it in the inspector, or near the start of your Start(), hook it up using GetComponent, but you’d also need a reference to your player object anyways.

V

Yes, I made it public and assigned the player object through the inspector. It works now.

Thanks for your help. :slight_smile:

1 Like

Yeah I saw that post of yours you quoted, was just asking if you corrected that problem since noting that. Anyways, good to hear it works

1 Like

For the record, if you ever want a private (or protected, whatever) field but want to keep the ease of ‘drag and drop to the inspector’, use the ‘SerializeField’ attribute over a variable. This will keep is modifier access, and allow you to use the inspector at the same time. :slight_smile:

3 Likes