Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 07, 2018 at 08:14 AM by Myth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Myth · Feb 14, 2011 at 03:28 AM · interactionvehicle

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;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Myth · Feb 14, 2011 at 10:04 AM 0
Share

unity demanded I do it how I have by not being able to identify the camera component.

avatar image Myth · Feb 14, 2011 at 10:06 AM 0
Share

Also I cannot figure out the correct syntax in order to use arrays - help on that topic would be great!

avatar image tool55 · Feb 14, 2011 at 02:35 PM 1
Share

I added to the answer above to try and explain built in arrays. Hope it's helpful

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges