Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by dragonking300 · Dec 11, 2016 at 05:48 AM · c#scripting beginnerbeginnerarrays

How would you move from a array like from [2] to [3] or backwards like [2] to [1]

So lets say I am on example[3] I want to iterate to example[2] and only when a certain condition is met it will count down to array [2] but if I met a different condition it will count up to example[4] this is what I want to do so I can have 360 degrees coverage by my main camera on my player . help? I will post if I manage to solve my self

using UnityEngine; using System.Collections;

 public class CameraController : MonoBehaviour
 {
 
     public GameObject player;
 
     private Vector3 offset;
 
     Vector3[] bawb = new Vector3[4]; 
 
     void Start()
     {
         offset = transform.position - player.transform.position;
         bawb[1] = new Vector3(10, 10, 10);
         bawb[2] = new Vector3(20, 20, 20);
         bawb[3] = new Vector3(30, 30, 30);
         bawb[4] = new Vector3(40, 40, 40);
 //camera positions(^  are test vectors)
         
     }
 
     void Update()
     {
         if (Input.GetKeyDown("q"))
         {
             //condition to interate a array number up
         }
         else
         {
          if (Input.GetKeyDown("e"))
             {
 //condition to interate a number down
             }
         }
     }
 
     void LateUpdate()
     {
         transform.position = player.transform.position + offset;
    //subtracting vectors to get preferd camera angle(no touchy >:( )
 }
 }

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 UnityCoach · Dec 11, 2016 at 08:04 AM 0
Share

what do you mean by iterating? once you have an array, you can loop through it, or simply use any of its values anytime.

avatar image Bunny83 · Dec 11, 2016 at 12:36 PM 0
Share

First of all, please format your code properly. If you don't know how, have a look at the user guide of UnityAnswers.

Second your question is extremely cryptic and contains many errors. First of all an array with 4 elements has the valid indices of 0, 1, 2 and 3. So in your case you never set the first element and when you try to set the element with index "4" you'll get an out-of-bounds exception.

The next strange things is your vectors are positions and you have arranged them in a line, yet you talk about rotation and 360° which makes no sense. Position and rotation are two completely different things.

Ins$$anonymous$$d of talking about array iterating up and down when some abstract conditions are met, you should simply describe as detailed as possible what you want as a result. You just explained a small part of your attempt but the actual problem is still unknown. Your code doesn't use your array anywhere so how should we deduct what you want to do?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by UnityCoach · Dec 11, 2016 at 08:08 AM

 for (int i = 10 ; i > 5 ; i--) // will go from 10 to 6 by 1
 {
 Debug.Log(theArray[i]);
 }
 
 for (int i = 10 ; i < 90 ; i+=10) // will go from 10 to 90 by 10
 {
 Debug.Log(theArray[i]);
 }
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
avatar image
0

Answer by dpoly · Dec 11, 2016 at 09:04 AM

The form of the question is pretty hard to understand, but with a few guesses...

The index into an array is an integer. To 'iterate up' increment the integer. To 'iterate down' decrement it.

 // make this an instance variable for the class
 private int indx = 3; // index for array[3]
 // these can go inside any method of the class
 indx++; // increment
 indx--; // decrement

If you go outside the range of the array, you will get a bounds error. So don't. You need to add code to check for that.

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 dragonking300 · Dec 11, 2016 at 03:27 PM 0
Share

Ok, i'm sorry for my Code post being confusing, I reformatted it so it would appear clear and I changed what I want my main camera to do to be clearer(witch is btw so that I can see from all sides of the player like 360 degree coverage so my game doesn't have to be a 2d platformer

Anyway, I can't seem to do bawb++ becuase it says I can't use a local variable before its declared?

avatar image dpoly · Dec 11, 2016 at 11:57 PM 0
Share

@dragonking300: You're asking really basic, beginner C# questions. You should probably take a break and go do a month or two working through C# tutorials before asking Unity questions. But see edit.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

258 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to display random questions without repeating the previous? 2 Answers

Get Childrens to new array from perents array. 1 Answer

arrow from text to object - complete beginner 0 Answers

Best way to learn how to utilize the Unity API? 2 Answers

How can i place subjects in different positions? 0 Answers


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