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 orlocuario · Apr 03, 2018 at 10:21 PM · scripting problemarrayarray of gameobjectsvaluesrevert

Order of elements in array going back to default after being set in different order

Hi community, thanks for the space in the first place.

I'm simply trying to create a system where I add GameObjects to a public array of GameObjects so I can create a movement circuit with all their positions. Its Called Circuit Builder.

I have an array of Game objects from which I create an array of Vector2 with all the gameObjects positions. Then I initialize each of the gameObjects via a script called "ObjectInCircuitMovementController" so they can start moving. I know I must change the order of the values in the array so each gameObject starts at its own spot and follow the circuit from his position.

Everything is working fine, the objects seem to be Initializing with the correct Vectors in the correct order, but when they start moving they all have the same order of elements in the array so they all go first to the same spot and then start moving.
I did breakpoints and debugs, they all say that each object is receiving the info correctly, but on the first Update, they all go back to the same order. Any ideas? Thanks. public class ObjectInCircuitMovementController : MonoBehaviour {

     public bool move;
     private Vector2[] targets;
     private float moveSpeed;
     private float timeToWait;
     private bool showedTargets;
     int i = 0;
 
     // Update is called once per frame
     void Update () {
         
         if (move)
             if (showedTargets == false)
             {
                 Debug.Log(gameObject.name + "has the following Targets" + targets[0] + ", " + targets[1] + ", " + targets[2] + ", " + targets[3]);
                 showedTargets = true;
             }
         {
             transform.position = Vector2.MoveTowards(transform.position, targets[i], moveSpeed * Time.deltaTime);
             ControlMovement();
         }
     }
 
     private void ControlMovement()
     {
         if (transform.position.x == targets[i].x && transform.position.y == targets[i].y)
         {
 
             i++;
 
             if (i == targets.Length)
             {
                 i = 0;
             }
         }
     }
 
     public void InitializeCyclicMovements(Vector2[] incomingTargets, float _moveSpeed, float _timeToWait)
     {
         string name = gameObject.name; 
         moveSpeed = _moveSpeed;
         timeToWait = _timeToWait;
         targets = incomingTargets;
         move = true;
         Debug.Log(gameObject.name + "has the following Targets" + incomingTargets[0] + ", " + incomingTargets[1] + ", " + incomingTargets[2] + ", " + incomingTargets[3]);
     }
 
     private IEnumerator WaitForMovement()
     {
         yield return new WaitForSeconds(timeToWait);
     }
 }
 
 
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 

public class CircuitBuilder : MonoBehaviour {

 public GameObject[] objectsToRotate;
 public float _moveSpeed;
 public float _timeToWait;
 protected Vector2[] objectPositions;


 // Use this for initialization

 void Start() {
     CheckParameters();
     SetStartPositionsInCircuit();
     InitializeMovementInEachObject();
 }

 private void InitializeMovementInEachObject()
 {
     for (int i = 0; i<objectsToRotate.Length; i++)
     {
         ObjectInCircuitMovementController cmController = objectsToRotate[i].GetComponent<ObjectInCircuitMovementController>();
         cmController.InitializeCyclicMovements(objectPositions, _moveSpeed, _timeToWait);
         ChangeObjectsPositionsOrderInArray();
     }

 }

 private void SetStartPositionsInCircuit()
 {
     objectPositions = new Vector2[objectsToRotate.Length];

     for (int i = 0; i < objectsToRotate.Length; i++)
     {
         Transform actualObject = objectsToRotate[i].GetComponent<Transform>();
         objectPositions[i] = actualObject.transform.position;
     }
 }

 private void CheckParameters()
 {
     if (objectsToRotate.Equals(default(GameObject[])))
     {
         Debug.LogError("Platform Rotator named: " + gameObject.name + " needs Objects to Start");
         return;
     }

     if (_moveSpeed.Equals(default(float)))
     {
         Debug.LogError("Platform Rotator named: " + gameObject.name + " needs a moveSpeed to Start");
         return;
     }
 }
 private void ChangeObjectsPositionsOrderInArray()
 {
     Vector2 savedPosition = objectPositions[0];

     for (int i = 0; i<objectPositions.Length; i++)
     {
         int j = i + 1;
         if (j < objectPositions.Length)
         {
             objectPositions[i] = objectPositions[j];
         }
         else if (j >= objectPositions.Length)
         {
             objectPositions[i] = savedPosition;
         }
     }
 }

}

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

· Add your reply
  • Sort: 
avatar image
0

Answer by orlocuario · Apr 03, 2018 at 10:33 PM

The initialize Debug throws that the values ahve been set correctly, Object 1: (W,X,Y,Z) Object 2: (X,Y,Z,W) Object 3: (Y,Z,W,X) Object 4: (Z,W,X,Y)

but the Update Debug throws all of the arrays with its values in the same order.

Object 1: (W,X,Y,Z) Object 2: (W,X,Y,Z) Object 3: (W,X,Y,Z) Object 4: (W,X,Y,Z)

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

199 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

Related Questions

how to check the bool of multiple object in an arrya. 0 Answers

Save a custom Class Array 0 Answers

Wher is the problem? My string isn't behaving... 1 Answer

Array index is out of range. How to solve? 1 Answer

Array sorting issues - Please Help!!! 4 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