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 Dag-Tholander · Feb 11, 2015 at 02:22 PM · arraypositionaddincreasereplay

Array doesn't increase or add element

I'm making a racing simulator and I made this script for a replay system. But when i tried it it didn't add any elements, they just stayed at 3. this is how it looked : alt text

 #pragma strict
 
 var recorded_obj : GameObject;//Object to record
 var Positions : Vector3[];
 var Rotations : Quaternion[];
 var replayPosition : int;
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
 AddPos();
 AddRot();
 if(Input.GetKey("r"))
 {
    Replay();
 }
 else
 {
    replayPosition = 0;
 }
 }
 
 function AddPos()
 {
 var positionsarr = new Array(recorded_obj.transform.position);
 positionsarr.length += 1;    
 positionsarr.Add(recorded_obj.transform.position);
 Positions = positionsarr;
 }
 
 function AddRot()
 {
 var rotationsarr = new Array(recorded_obj.transform.rotation);
 rotationsarr.length += 1;    
 rotationsarr.Add(recorded_obj.transform.position);
 Rotations = rotationsarr;
 }
 
 function Replay()
 {
 //Position
 recorded_obj.transform.position = Positions[replayPosition];
 //Rotation
 recorded_obj.transform.rotation = Rotations[replayPosition];
 
 replayPosition += 1;
 }

What's wrong? I've read the Add() description in the scripting API, and it says that it adds a for example value to the end of the Array.

array.jpg (54.5 kB)
Comment
Add comment · Show 1
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 meat5000 ♦ · Feb 11, 2015 at 02:23 PM 0
Share

Line 39

  rotationsarr.Add(recorded_obj.transform.position);

Spot the mistake.

1 Reply

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

Answer by meat5000 · Feb 11, 2015 at 02:22 PM

Use a List

  #pragma strict
  
  import System.Collections.Generic; //Access Lists
  
  var recorded_obj : GameObject;//Object to record
  public var positionsarr : List.<Vector3>; //Make List of type
  public var rotationsarr : List.<Quaternion>; //Make List of type
  var Positions : List.<Vector3>;
  var Rotations : List.<Quaternion>;
  var replayPosition : int;
 
  function Update () 
  {
      AddPos();
      AddRot();
      if(Input.GetKey("r"))
      {
         Replay();
      }
      else
      {
         replayPosition = 0;
      }
  }
  
  function AddPos()
  {  
      positionsarr.Add(recorded_obj.transform.position);
      Positions = positionsarr;
  }
  
  function AddRot()
  {   
      rotationsarr.Add(recorded_obj.transform.rotation);
      Rotations = rotationsarr;
  }
  
  function Replay()
  {
      recorded_obj.transform.position = Positions[replayPosition];
      recorded_obj.transform.rotation = Rotations[replayPosition];
      replayPosition += 1;
  }


Note : If you experience a slowdown with this script, its because the editor is trying to update everything to the inspector. Hide the values from inspector or simply close the List fields by clicking the triangles.

Comment
Add comment · Show 1 · 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 Dag-Tholander · Feb 11, 2015 at 08:34 PM 0
Share

@meat5000 Thanks! it works fine now. i really appreciate yor help

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NGUI - Vector 3 Position different from position 1 Answer

Adding to an array - C# 2 Answers

problem moving a prefab object in script (c#) 0 Answers

Make object move in a direction depending on where it spawns? (C#) 1 Answer

How to set to game objects's position from 2 different game objects arrays equal to each other? 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