- Home /
The question is answered, right answer was accepted
Stopping sctipts interacting
Hi,
I have been trying to create a script that I can use on every vehicle in my game which works fine until it is on two objects.
As soon as it is on two objects the second script (to be put onto an object) no longer switches camera.
Each vehicle will have up to 7 cameras ( hence testing if camera is there )
How do I stop the script interacting with other versions of itself?
// seat position var driver_seat : Transform; var seat_1 : Transform; var seat_2 : Transform; var seat_3 : Transform; var seat_4 : Transform; var seat_5 : Transform; var seat_6 : Transform;
// turret and ship cams var ship_cam : Camera; var cam1 : Camera; var cam2 : Camera; var cam3 : Camera; var cam4 : Camera; var cam5 : Camera; var cam6 : Camera;
// timer var timer : int = 30;
// ship var ship : GameObject;
// active control vars private var state : int = -1;
// player position & cam var play : Transform; var player_cam : Camera;
function Update () { timer = timer - 1;
if ( Input.GetAxis ("e_key") && state == -1 && timer < 0)
{
timer = 30;
if (driver_seat != null)
{
if(Vector3.Distance (driver_seat.position, play.position ) < 1.5 )
{
state = 0;
ship.BroadcastMessage ("Activate_Turret", 0);
}
}
//
if (seat_1 != null)
{
if(Vector3.Distance (seat_1.position, play.position ) < 3 )
{
state = 1;
ship.BroadcastMessage ("Activate_Turret", 1);
print ("a");
}
}
//
if (seat_2 != null)
{
if(Vector3.Distance (seat_2.position, play.position ) < 1.5 )
{
state = 2;
ship.BroadcastMessage ("Activate_Turret", 2);
}
}
//
if (seat_3 != null)
{
if(Vector3.Distance (seat_3.position, play.position ) < 1.5 )
{
state = 3;
ship.BroadcastMessage ("Activate_Turret", 3);
}
}
//
if (seat_4 != null)
{
if(Vector3.Distance (seat_4.position, play.position ) < 1.5 )
{
state = 4;
ship.BroadcastMessage ("Activate_Turret", 4);
}
}
//
if (seat_5 != null)
{
if(Vector3.Distance (seat_5.position, play.position ) < 1.5 )
{
state = 5;
ship.BroadcastMessage ("Activate_Turret", 5);
}
}
//
if (seat_6 != null)
{
if(Vector3.Distance (seat_6.position, play.position ) < 1.5 )
{
state = 6;
ship.BroadcastMessage ("Activate_Turret", 6);
}
}
}
// get out of turrets
if ( Input.GetAxis ("e_key") && state > -1 && timer < 0)
{
state = -1;
timer = 30;
BroadcastMessage ("Activate_Turret", -1);
}
// switch cameras
// player cam
if (state == -1)
{
player_cam.GetComponent("Camera").active = true;
}
else
{
player_cam.GetComponent("Camera").active = false;
}
// cam 0
if (ship_cam != null)
{
if (state == 0)
{
ship_cam.GetComponent("Camera").active = true;
}
else
{
ship_cam.GetComponent("Camera").active = false;
}
}
//cam 1
if (cam1 != null)
{
if (state == 1)
{
cam1.GetComponent("Camera").active = true;
}
else
{
cam1.GetComponent("Camera").active = false;
}
}
//cam 2
if (cam2 != null)
{
if (state == 2)
{
cam2.GetComponent("Camera").active = true;
}
else
{
cam2.GetComponent("Camera").active = false;
}
}
}
Answer by tool55 · Feb 14, 2011 at 05:32 AM
I think the reason you're not getting an answer is that the script is overly complex and we don't know where your variables come from. For example, you have all the camera variables declared at the top, yet you drill through the hierarchy and use GetComponent to find them when you've already got them defined. Why not just use:
cam1.active = true;
And we don't really know what you're trying to achieve. When you say you have multiples of the same script on different vehicles, does that mean that each vehicle has 7 cameras of it's own?
It may be that a master script would work better for what you're doing, but it's hard to tell without more information. You might also benefit from using arrays for so many objects and perhaps switch statements for all of the states that you seem to have.
Here's the basic set up for built in arrays:
myCams : Camera[]; mySeats : GameObject[]; myTurrets : GameObject[];
function Update()
{
if (Input.GetKeyDown("e"))
{
for (var thisSeat in mySeats)
{
//if thisSeat is close to the player do this
}
}
}
In the inspector you'll see a myCams array slot asking for length. Enter the number of cameras (or other objects or variables of the same type) into that slot and a drop down list will appear with variable slots for all of them. You can access individual variables in an array by referencing their index number
myCams[0]
myCams[1]
Arrays number from 0 - whatever length of the array is. You can also use a variable instead of the index number, which might work in your case since you want to activate a few things with the same number.
var numberToActivate : int;
myCams [numberToActivate] mySeats [numberToActivate]
If you have six cameras and six seats and the index numbers match, you can use the variable to access these matching game objects. Not sure this is clear, but you may be able to save a lot of code this way. Arrays in general can get a bit tricky, but built in arrays are quite easy to use. The Unity folks have seen to it. Hope this helps
unity demanded I do it how I have by not being able to identify the camera component.
Also I cannot figure out the correct syntax in order to use arrays - help on that topic would be great!
I added to the answer above to try and explain built in arrays. Hope it's helpful
Follow this Question
Related Questions
Problem with vehicle script 1 Answer
Vehicle Script Debugging 1 Answer
Vehicle/car/ Movement 0 Answers
Car goes a bit left when moving 1 Answer
Vehicle (tank) for my character 1 Answer