- Home /
Cannot disable a script from another script
I have been trying to find the solution for the past hour. I have a chicken enemy in my Game. One script, "Randomnpcmovement" controls the movement and attacking. The other, "generictakedamage" controls health and death. I am trying to use "generictakedamage" to disable "Randomnpcmovement" once health is at zero or less. But for all the combinations of syntax I have tried, it keeps getting rejected by Unity. Both scripts are in the same object called "TempChicken". The entire content of "generictakedamage" is as follows:
var health : float = 300;
var go = Randomnpcmovement.Find(
var randomMovement: Randomnpcmovement = GetComponent(Randomnpcmovement);
function Start (){
}
function OnCollisionEnter(whatIHit : Collision)
{
health -= 10;
print ("Chickenshot");
}
function Update (){
if (health <= 0) {
randomMovement.GetComponent("Randomnpcmovement").enabled = false;
}
}
Also, next time post more cleanly. I've fixed it up for you this time, but I can't do it for you all the time. Remember that when you are writing a question, it gives you a preview below the text-entry field. You can use this to make sure that what you are posting looks right.
Answer by syclamoth · Jan 24, 2012 at 03:54 AM
The problem here is where you are using the GetComponent function. You can't use lookup functions that depend on the current state of the object in class-variable declarations, because they need to be evaluated at compile-time, not at runtime!
You should get rid of the 'go' variable from the top, and instead of trying to find the component for RandomMovement during field initialisaion, do it in Start:
private var randomMovement : randomnpcmovement;
function Start() { randomMovement = GetComponent(randomnpcmovement); }
Then when you want to access it in Update, just use randomMovement:
function Update (){
if (health <= 0) {
randomMovement.enabled = false;
}
}
I have done as you asked. Now I am receiving the error:
Assets/generictakedamage.js(2,30): BCE0018: The name 'Randomnpcmovement' does not denote a valid type ('not found'). Did you mean 'UnityEngine.RuntimePlatform'?
I'm unsure how to proceed.
Well, that's up to you. $$anonymous$$ake sure that the filename of the script you are trying to get is the same as the name in the GetComponent function- I can't really help you if you have spelled the names wrong in your original question post!
I double checked and both are spelled correctly. Here's a screenshot proving it: http://imgur.com/nigZx
Fix that stuff at the top of your script where you declare the variable 'random$$anonymous$$ovement' twice (once as private, the next with GetComponent)... make only ONE random$$anonymous$$ovement variable and only set it as variable : ScriptName ; ..then in start set it with the getcomponent like @syclamoth showed above.
Then, take a closer look at your image in the inspector panel.
Next to the checkbox for enabling the script, it's showed as "Randomnpcmovement" ...BUT... directly beneath that in the script slot, your script is actually named "randomnpcmovement" with all lower-case letters. The editor displays some things differently for easier reading. $$anonymous$$ake sure you're using the actual script name.
var random$$anonymous$$ovement : randomnpcmovement ;
function Start(){
random$$anonymous$$ovement = GetComponent(randomnpcmovement) ;
}
Look at your Generictakedamage display in that same image.. you've also named that script with all lower-case and would have caused the same problem you're having here. It's better practice to capitalize ScriptNames anyway. RandomNpc$$anonymous$$ovement ; GenericTakeDamage .
Answer by Grug · Jan 24, 2012 at 05:39 AM
This is for a project that is due tomorrow. I would appreciate help, especially since it seems to be a Unity problem.
No, it's definitely your fault. See the comment at the end of my answer. It's a spelling mistake. Spelling is very important when you're program$$anonymous$$g.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063327im_/https://answers.unity.com/themes/thub/images/avi.jpg)