- Home /
disabling a script when collide with a cube
Hi, i want to make an enemy behind a corner come out and follow you when you step on a cube. I have all the scripts but this one doesn't work i dont know why here's my script:
function OnControllerColliderHit(hit:ControllerColliderHit) {
var script : EnemyAI;
var script : EnemyAttack;
if(hit.gameObject.tag == "Instantiate enemy")
{
GetComponent(EnemyAI).enabled = true;
GetComponent(EnemyAttack).enabled = true;
}
}
what should i do? the enemy has EnemyAI and EnemyAttack disabled and when you step on a cube i want them to be enabled but i get 2 errors:
Assets/Scripts/Enable script.js(4,14): BCE0018: The name 'EnemyAI' does not denote a valid type ('not found'). Did you mean 'System.Enum'?
Assets/Scripts/Enable script.js(5,14): BCE0018: The name 'EnemyAttack' does not denote a valid type ('not found').
Do you have a script named "EnemyAI" and a script named "EnemyAttack"?
If you do - is it in Javascript? C#?
Where is it located and where is the script you have shown us your code from is located?
just a check, but this script isn't running from the disabled script?
Answer by kieblera5 · Jul 07, 2011 at 04:48 PM
Please cache the components...
var enemyAIScript : EnemyAI;
var enemyAttackScript : EnemyAttack;
function Start()
{
enemyAIScript=gameObject.GetComponent.<EnemyAI>();
enemyAttackScript=gameObject.GetComponent.<EnemyAttack>();
}
function OnControllerColliderHit(hit:ControllerColliderHit)
{
if(hit.gameObject.tag == "Instantiate enemy")
{
enemyAIScript.enabled = true;
enemyAttackScript.enabled = true;
}
}
Also note that the these scripts must be attached to the object that this script is attached to. If not, you will first have to cache the enemy object, find it, and then get the component from that object
Answer by GuyTidhar · Jul 07, 2011 at 05:08 PM
Leave this script where it is and move your two C# scripts (EnemyAI, EnemyAttack) under a folder named: "Plugins" - create one if it does not exist yet).
This should work. I didn't know that they were C# scripts. Either way, take note of what I said and cache your components, so that you don't end up using GetComponent multiple times.
And this script you wrote must be attached to my enemy or something else?
Also note, always move things around inside of the Project View inside of Unity. Never move them around in the folders on your computer.
The way I wrote my script, I assumed all the scripts were attached to the same GO. If not you will have to find the GO and pull it's component ins$$anonymous$$d of the current GO.
Answer by dibonaj · Jul 07, 2011 at 05:16 PM
Take a look at this: http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
Unity compiles scripts in a certain order. So if you are ever trying to access C# from JavaScript or the other way around, you need to make sure you have them in the proper location. Either move your C# script to a folder called Plugins or move your JavaScript files to the Standard Assets folder.
Thank you soooooooooo much it worked you are the definition of awesome sauce. I just have one more question, i have to add the enemy to the script in the inspector but i have to do it in run time, how can i make it work right when i click the play button and not have to add all the stuff in the incpector?
I just noticed that i get an error that can be erased NullReferenceException: Object reference not set to an instance of an object Enable script.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/Scripts/Enable script.js:14) UnityEngine.CharacterController:$$anonymous$$ove(Vector3) Character$$anonymous$$otor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.js:229) Character$$anonymous$$otor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.js:331)
what does it mean?
For your first question, you should probably start up a new thread and ask that question. Also, if you can select multiple answers as the correct answer, then select $$anonymous$$e and GuyT since they basically say the same thing. If not, select his since he said it first.
Now, can you update your original post at the top with the script that the error takes you to? I need to know which line it is.