- Home /
Problem is not reproducible or outdated
Get Component Error
I'm having difficulty with this script. It is made to set a separate script as disabled once the main script is enabled. I've checked the script names, they are all correct.
using UnityEngine;
using System.Collections;
public class PowerDown : MonoBehaviour {
public GameObject BotScript;
// Use this for initialization
void OnEnable () {
BotScript.GetComponent<BotMovement>().enabled = false;
}
// Update is called once per frame
void Update () {
}
}
Please help :/
The botscript is the object that the script I want to disable is in.
When asking for help with an error, you must include the full error message and the line number to which it relates. Otherwise, we're just guessing, and there is never any reason to guess what the problem is when debugging code.
Answer by Bunny83 · Aug 20, 2017 at 06:52 PM
There is no problem with your code as long as you actually assigned the correct gameobject to the "BotScript" variable. However it would be way better to use a variable of type "BotMovement" instead of "GameObject". That way you don't need the GetComponent at all.
public BotMovement BotScript;
void OnEnable () {
BotScript.enabled = false;
}
Of course if you change the variable type you have to re-assign your object. This is much safer as it doesn't rely on dynamically looking up if that component exists. You directly reference your target script at edit time.
unless maybe it is because you can't disable a java script through a c# script? idk.
Uhm you haven't mentioned that in your question. You also did not mention the actual error. So you talk about a compiler error and not a runtime error?
You should avoid mixing UnityScript (Unity's Javascript) and C#. Both languages are compiled independently. Read this page carefully, especially the bit at the bottom about the compilation phases. Scripts in phase 3 can always access everything from phase 1 but not the other way round.
Follow this Question
Related Questions
GetComponent Picking up multiple scripts from differrent objects? 1 Answer
"Error building Player" WebGL build error 2 Answers
How do I check if the gameobject I collided with has a certain script? (C#) 1 Answer
Game Analytics & Facebook sdk android build error 1 Answer
GetThreadContext failed (Unity 5) 0 Answers