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 VNMSSkiddzz · Jan 19, 2016 at 09:37 AM · unity 5scripting problemarray of gameobjects

How to make objects appear in different positions?

Hello,

So I have a 3 x 3 gameboard with 9 items that I'm trying to place and after a certain time, they switch places. I've created a game objects array as well as a vector3 array to hold all possible positions. I then tried to set a timer to have the items swithc places after a certain time, but it doesn't work. My items are listed as item1, item2, etc. under a parent object called items. My script is on the parent item. Here's the code I've been trying:

using UnityEngine; using System.Collections;

public class Randomize : MonoBehaviour {

 private Vector3[] positions;
 public GameObject[] items;
 int n;
 float timer = 0;

 // Use this for initialization
 void Start () {

     positions = new Vector3 [items.Length];
     positions [0] = new Vector3 (-2, 3, 0);
     positions [1] = new Vector3 (0.5f, 3, 0);
     positions [2] = new Vector3 (3, 3, 0);
     positions [3] = new Vector3 (-2, 0.5f, 0);
     positions [4] = new Vector3 (0.5f, 0.5f, 0);
     positions [5] = new Vector3 (3, 0.5f, 0);
     positions [6] = new Vector3 (-2, 2, 0);
     positions [7] = new Vector3 (0.5f, -2, 0);
     positions [8] = new Vector3 (3, -2, 0);

     items = GameObject.FindGameObjectsWithTag ("items");

     for (int i = 0; i < items.Length; i++) {
         positions [i] = items [i].transform.position;
     }
 
     }
     
     
 // Update is called once per frame
 void Update () {
     var n = Random.Range (0, 8);
     timer = Time.deltaTime;
     if (timer > 1) {
         positions [n] = items [n].transform.position;
     }
 }

}

Any suggestions on how to fix it and what I'm doing wrong?

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 corn · Jan 19, 2016 at 12:00 PM

Let's take a look at this Update method.

  void Update () {
      var n = Random.Range (0, 8);
      timer = Time.deltaTime;
      if (timer > 1) {
          positions [n] = items [n].transform.position;
      }
  }

First of all, Time.deltaTime is the time it took to complete the last frame. It's not supposed to be greater than 1 at any given time.

What you meant to do was probably :

 timer += Time.deltaTime;

Which would make way more sense : every frame, you add the elapsed time to your timer.

But then there's another mistake : positions [n] = items [n].transform.position;

This won't move your GameObject, this will only assign its current position to the nth entry of your positions array. If you want to move your GameObject, you should do the exact opposite : items [n].transform.position = positions [n]; You should probably fix that in Start too.

So what you should write is :

  void Update () 
  {
      var n = Random.Range (0, 8);
      timer += Time.deltaTime;
      if (timer > 1) 
      {
          items [n].transform.position = positions [n];
      }
  }
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

49 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

Related Questions

setting up roommanager 0 Answers

Score value in GameOver screen is 0 instead of final value from the Game Level scene(used PlayerPrefs), how do I display the final value from Game Level scene in the GameOver scene? 1 Answer

How to get variables of a behavior tree with RAIN AI 0 Answers

NullReferenceException on getComponent 1 Answer

Playerprefs are not deleting when an apk is uninstalled? 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