What is wrong with my script?
Hello, I am having some problems with the script below: #pragma strict var Player : GameObject; var Chair : GameObject; var Chair2 : GameObject; var Chair3 : GameObject; var Chair4 : GameObject; var Table : GameObject; var Table2 : GameObject; var Table3 : GameObject; var Table4 : GameObject; var Table5 : GameObject; var Blackhole : GameObject; var VortexStop : AudioClip; private var Vortex : Wormhole; private var ResetLevelScript : ResetLevel;
function Start ()
{
Vortex = Player.GetComponent(Wormhole);
Vortex = Chair.GetComponent(Wormhole);
Vortex = Chair2.GetComponent(Wormhole);
Vortex = Chair3.GetComponent(Wormhole);
Vortex = Chair4.GetComponent(Wormhole);
Vortex = Table.GetComponent(Wormhole);
Vortex = Table2.GetComponent(Wormhole);
Vortex = Table3.GetComponent(Wormhole);
Vortex = Table4.GetComponent(Wormhole);
Vortex = Table5.GetComponent(Wormhole);
ResetLevelScript = Blackhole.GetComponent(ResetLevel);
}
function OnTriggerEnter (Trigger : Collider)
{
if(Trigger.gameObject.tag == "Player")
Vortex.enabled = false;
ResetLevelScript.enabled = false;
GetComponent.<AudioSource>().clip = VortexStop;
GetComponent.<AudioSource>().Play();
}
The problem I am having is that I want to disable the same script for multiple objects, but when my player passes through the trigger, only one object (table 5) has the script disabled. Is there a different method to disabling the scripts in multiple objects rather than just one? Thank you for any help, Ayato Naoi.
Answer by Jinxology · Sep 28, 2015 at 09:48 PM
That's because Table5 is the last thing you assign to Vortex. You keep overwriting Vortex with every assignment until it ends on Table5.
I'm not sure which object you have assigned that script to, but you need to have it on each of your objects that you want to disable when player collides with it.
Small best practice criticism, it helps to have your variables start with lowercase letters to distinguish them as variables and not object types (ie. use vortex instead of Vortex).
I should have added more detail to the description, but I have the Wormhole Script on all of the variables listed other than the Blackhole. I have this script on a box collider object which is marked as the trigger. The effect I was hoping for was that of one where all of the Wormhole scripts are disabled as soon as the player passes through the trigger.
Can you lay out the bigger picture for me, what is your game? Also, rather than setting .enabled=false, I would recommend setting .isTrigger = false
For clarity, I'm having trouble visualizing your game... wormholes and blackholes coupled with tables and chairs?
Your answer
Follow this Question
Related Questions
Identical Lines of Code Will Either Disable, or Wont Disable a Script. Need Help. 0 Answers
Disable an added script from First Person Character 0 Answers
Disable a script for a few seconds 2 Answers
How to enable and disable a script on an game object by pressing a keyboard button? 1 Answer
How to disable my buttons when i press and enable the other ones? 0 Answers