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 16, 2011 at 01:44 AM · arraysfor-loop

Array Syntax Issue

I have this piece of code that works EXCEPT for the part that activates the camera from the array myCams.

// activate other camera
myCams[thisSeat].GetComponent("Camera").active = true;

All my attempts at getting this line of code to work have resulted in massive amounts of errors.

Thanks in advance for any help!

here is the full code:

// arrays var myCams : Camera[]; var mySeats : Transform[];

// timer var timer : int = 30;

// ship var ship : GameObject;

// active control vars private var state : int = -1; private var count : int = 0;

// player position & cam var player_object : Transform; var player_camera : Camera;

function Update() { // fix speed timer = timer - 1;

 if (Input.GetAxis ("e_key") && state == -1 && timer < 0)
 {
     for (var thisSeat in mySeats)
     {
         // add to count so we know what seat to broadcast
         count = count + 1;

         // test for proximity to a seat
         if(Vector3.Distance (thisSeat.position, player_object.position ) < 3 )
         {
             // deactivate first person camera
             player_camera.GetComponent("Camera").active = false;

             // activate other camera
             myCams[thisSeat].GetComponent("Camera").active = true;

             // now sitting at
             state = count - 1;

             // tell turret / driving scripts they can activate
             BroadcastMessage ("Activate_Turret", state);
         }
     }

     // reset count
     count = -1;
 }
 if (Input.GetAxis ("e_key") && state > -1 && timer < 0)
 {
     // we are now first person again
     state = -1;

     // reset timer
     timer = 30;

     // tell turret / driving scripts they can deactivate
     BroadcastMessage ("Activate_Turret", -1);

     // reactivare first person camera
     player_camera.GetComponent("Camera").active = true;

     // disable all other cameras
     for (var thisCam in myCams)
     {
         thisCam.GetComponent("Camera").active = false;
     }
 }

}

Comment
Add comment · Show 2
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 e-bonneville · Feb 16, 2011 at 01:59 AM 0
Share

What error are you getting? NullReferenceException?

avatar image Peter G · Feb 16, 2011 at 03:22 AM 1
Share

Getting the camera component on an array of cameras is redundant.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 16, 2011 at 03:39 AM

Your "for loop" is a so called "for each" loop. The loop var thisSeat receives the current element of the array. Since the mySeats array is of type Transform thisSeat is also a Transform.
You should use typed variables, because they are faster and it's clearer what they actually do.

for (var thisSeat : Transform in mySeats)

Ok, that's was the first thing i've seen. Now your "real" problem:
You try to use a variable of type Transform as an int index into the cam-array. That's impossible ;)

For each loops are quite handy because you don't have to deal with index variables and so on, but inside the loop you can't determine the actual index of the current element. You can always just iterate through one array at a time.

You have to do it the "old" traditional way ;)
( not 100% sure whether the syntax is correct or not, i'm not a JS user :P )

for (var index : int = 0; index < mySeats.Length; index++)
{
    if(Vector3.Distance (mySeats[index].position, player_object.position ) < 3 )
    {
        // deactivate first person camera
        player_camera.camera.active = false;
        // activate other camera
        myCams[index].camera.active = true;
        // now sitting at
        state = index;
        // tell turret / driving scripts they can activate
        BroadcastMessage ("Activate_Turret", state);
        // break out of the for loop cause we have already selected one seat
        break;
    }
}
Comment
Add comment · Show 2 · 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 21, 2011 at 03:42 AM 0
Share

you must use enabled NOT active

avatar image Myth · Feb 21, 2011 at 03:43 AM 0
Share

Have posted the final code below

avatar image
0

Answer by Myth · Feb 21, 2011 at 03:41 AM

// arrays var myCams : Camera[]; var mySeats : Transform[];

// timer var timer : int = 3;

// ship var ship : GameObject;

// active control vars private var state : int = -1; private var count : int = 0;

// player position & cam var player_object : Transform; var player_camera : Camera; var old_position : Vector3;

function Update() {

 if (Input.GetAxis ("e_key"))
 {

     if (state == -1 &amp;&amp; timer &lt; 0)
     {

         for (var index : int = 0; index &lt; mySeats.Length; index++)
         {

             if(Vector3.Distance (mySeats[index].position, player_object.position ) &lt; 3 )
             {
                 // deactivate first person camera
                 player_camera.enabled = false;

                 // activate other camera
                 myCams[index].enabled = true;

                 // now sitting at
                 state = index;

                 print (index);

                 // tell turret / driving scripts they can activate
                 BroadcastMessage ("Activate_Turret", state);

                 // reset timer
                 timer = 20;

                 // parent user to seat
                 player_object.parent = mySeats[index];

                 old_position = player_object.transform.localPosition;

                 player_object.transform.localPosition = Vector3(0, 0, 0);

                 // break out of the for loop cause we have already selected one seat
                 break;
             }
         }
     }

     // get out
     if (state &gt; -1 &amp;&amp; timer &lt; 0)
     {
         // we are now first person again
         state = -1;

         // reset timer
         timer = 30;

         // tell turret / driving scripts they can deactivate
         BroadcastMessage ("Activate_Turret", -1);

         // put back relitave to where the player entered the vehicle/seat
         player_object.transform.localPosition = old_position;

         // Detaches the transform from its parent.
         transform.parent = null;

         // reactivare first person camera
         player_camera.GetComponent("Camera").enabled = true;

         // disable all other cameras
         for (var thisCam in myCams)
         {
             thisCam.GetComponent("Camera").enabled = false;
         }
     }
 }

 // fix speed
 timer = timer - 1;

}

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is for loop caching worth it in unity3d Mono Develop 1 Answer

Get child position from last gameobject in array 2 Answers

How do I alternate the colors of my Chessboard? 2 Answers

Clicker game instantiate prefab depending on gold per click 0 Answers

Array index is out of range? 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