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 /
avatar image
0
Question by Khozo · Nov 11, 2014 at 11:48 AM · arrayobjectvector3transform.position

Place object in different positions when L is clicked

I'm trying to make an array of positions so once L is clicked, it goes to position (12, 3, 4) then when I click L again I can make it go to (7, 3, 5).

 var position : Vector3 = transform.position;
 private var index : int;
 
  function Update () {
      if(Input.GetKeyDown("l")){
          index++;
          index = index % position.Length;
     transform.position = position[index];
      }
  }

I'm inexperienced with javascript so it may be something obvious :s

Edit: It says "move.js(8,34): BCE0022: Cannot convert 'float' to 'UnityEngine.Vector3'. What I want it to do is to let me choose how many different positions I want (The size) and let me input the positions into each one. Thus everytime I click L, it picks the next position in the array.

Comment
Add comment · Show 5
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 AlwaysSunny · Nov 11, 2014 at 11:15 AM 0
Share

Why would the index become the remainder of a division of itself by the array length? Anyhow, there's nothing overtly wrong here, except quite possibly that. When posting questions, please specify what is happening, and what you want to happen ins$$anonymous$$d.

avatar image Khozo · Nov 11, 2014 at 11:21 AM 0
Share

Edited with new information.

avatar image AlwaysSunny · Nov 11, 2014 at 11:28 AM 0
Share

I'm thinking you're misusing arrays. Where do you want to input positions? In the inspector? I dunno squat about .js, but here's the reference page for working with .js Arrays. http://docs.unity3d.com/ScriptReference/Array.html

To pick the next position in the array, just increase the index by one. When posting error messages, please paste the entire error so that we can see the line number - the first number in a pair in parentheses. That'll also help you locate problems yourself.

avatar image Khozo · Nov 11, 2014 at 11:55 AM 0
Share

Thank you! This works perfectly!

avatar image MrSoad · Nov 11, 2014 at 11:59 AM 0
Share

I will convert it to an answer then, would be nice if you accept it :D Thanks in advance if you do :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MrSoad · Nov 11, 2014 at 11:43 AM

If you only want the two different positions then you do not really need an array, but below is how you would do what I think you want using an array(I've not tested this code but I think I've written it right).

 //Decalare your Positions Array.
 private var aPositions : Vector3[];
 
 //Your switch position flag var.
 private var bSwitch : boolean = false;
 
 function Start() {
 
     //Set the size of your positions array, 2.
     aPositions = new Vector3[2];
     
     //Fill your positions array with your two positions
     subFill_Array();
 }
  
 function Update () {
 
     //If "l" key pressed switch position.
     if (Input.GetKeyDown("l")) {
     
         if (bSwitch) {
             transform.position = aPositions[0];
             bSwitch = false;
         } else {
             transform.position = aPositions[1];
             bSwitch = true;
         }
     }
 }
 
 function subFill_Array() {
 
     aPositions[0] = Vector3(12, 3, 4);
     aPositions[1] = Vector3(7, 3, 5);
 }
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Linking array of sets of vector3's together 1 Answer

Is there any option to put objects together? 0 Answers

How to make flat object(Unity 2D) 1 Answer

Assigning to Vector3 array. 1 Answer

if close to one of a kind? 2 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