- Home /
Find Script from another script
Ok so i have a 2 game objects, Object A & Object B
Object A is a player controlled character with quite a few scripts attached. Object B is a trigger Zone
On Object A there are 2 animation sets, 1 Unarmed 2 Armed
So when i step over the trigger zone, i have the following:
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Player"){
GetComponent(Unarmed).enabled = false;
GetComponent(Armed).enabled = true;
}}
Now when i do a debug test to see if its detecting the collision all works well. Basically the Armed script starts off inactive and when i touch the trigger zone i want it to disable and enable 2 scripts. It doesn't seem to work, I see it as i need to somehow link scripts between 2 seperate prefabs or objects, How can i do this? Thanks in Advance.
right i have to ask the other two scripts are they attached to the same player or other objects?
See if the answer that i have posted it might help you :)
Answer by MC HALO · Apr 24, 2011 at 04:37 PM
Ok say for example you have a player right and on this player you have 5 scripts, now in the game you also have 3 other players with 2 scripts. now you said if you step on the trigger you disable one script and enable the other. because i don't know if these scripts are all attached to your player i will still give you an answer that can help :
function OnTriigerEnter (hit: Collider) {
if(hit.gameObject.tag =="Player"){
// this will disable the script on the player
GameObject.Find("Player").GetComponent("put the script name here ").enabled = false;
// this will enable the script on the enemy
GameObject.Find("Enemy1").GetComponent("put the script name here ").enabled = true;
// this will enable the script on the enemy
GameObject.Find("Enemy2").GetComponent("put the script name here ").enabled = true;
}
}
Now basically all you need to do is tell the script what gameOject it should look for the script on and then which script is should enable and disable
i hope this help :)
Yep, that's the way. Just keep in $$anonymous$$d that GameObject.Find isn't very realiable. If you know in advance what object's you'll be referencing, just add them in manually using var Player : GameObject;, etc.
Yep that's true Joshua find is not very reliable. i totally agree with you make them in to a variable and then do the rest this way its a lot better because sometime people forget there tags this way all you have to do is drag and drop the right object in the inspector mode and just cal:)l them :) very good comment
Works great thats exactly what i was looking for, thanks very much :)
I tried that and it said there was no such thing as 'enabled.' I thought maybe you mistaked it for active, but even though it existed. that didn't work either. Are you using C# or JS?
Answer by Joshua · Apr 24, 2011 at 04:24 PM
As MC HALO said, we need to know what the referenced scripts are attached to. If they are not on the gameObject the script you posted is executing from - then that's what you're doing wrong. The script now searches the game object it's attached to for components it does not have.
Yeh Components are both on seperate items Object A has both animation scripts while Object B only has the trigger script So how can i link them?