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 /
avatar image
0
Question by Amarixb · Sep 14, 2016 at 12:33 PM · movementarraypositionvector3button trigger events

Moving an Object to the vector of other objects on button press using a vector3 array?

OK! so. I'm currently working on a game where I there are multiple places for a user to place a game piece. Sort of like chess. Because it's in 3D I am struggling with the having set locations for the game pieces to be placed and so far what I've got is code that is supposed to place a marker at the currently selected location and switch to the next available location on button press.

I have placed invisible squares (just with the mesh toggled off) on the game board and have given them all the tag "space" my code is written to first take them into a GameObject array then I've created a loop that goes through each game object (these are selected using the "space" tag) and it puts their Vector3 values into another array that I can call each space from using each specific index.

This was supposed to call the vector3 at index[x] and then translate.transform my sphere marker object? but it just puts the sphere at 0.0f 0.0f 0.0f...

is there an easier way to do this?? -___-;;;! Ty so much for the help! sorry if this is all crazy I'm only just starting my second year at school for programming so I still know very little about this madness.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI; // <--- for button behavior
 
 public class CreateSprite : MonoBehaviour {
 
     public Button myButton;
     public Vector3[] characterSpacePostion = new[] { new Vector3() };
 
     // create an int to know how many times you've pushed this button
     public int buttonPress = 0;
 
     // Use this for initialization
     void Start () {
 
         // create an array for each object
         GameObject[] characterSpaceOptions = GameObject.FindGameObjectsWithTag("space");
 
         // create an array for each position
        
         // sort through each objects with the tag "space"
         for (var i = 0; i < characterSpaceOptions.Length; i++)
         {
             // get the currently selected objects position
             Vector3 nextVect = characterSpaceOptions[i].transform.position;
 
             // add the curerrent objects vector 3 to the array at the same position
             characterSpacePostion[i] = new Vector3(nextVect.x, nextVect.y, nextVect.z);
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         // find all objects with the tag "space"
 
         myButton = GetComponent<Button>(); // <-- you get access to the button component here
 
         myButton.onClick.AddListener(() => { scrollThroughSpaces("stringValue", 4.5f); });  // <-- you assign a method to the button OnClick event here
         // myButton.onClick.AddListener(() => { myAnotherFunctionForOnClickEvent("stringValue", 3); }); // <-- you can assign multiple methods
     }
 
     // use a method to find each object with specific tag then 
     // use a for statement to get each objects location.
     void scrollThroughSpaces(string argument1, float argument2)
     {
         /**  // create an array for each object
                 GameObject[] characterSpaceOptions = GameObject.FindGameObjectsWithTag("space");
                 
                 // create an array for each position
                 Vector3[] characterSpacePostion = new[] { new Vector3() };
 
 
                 // your code goes here
                 // example ----> print(argument1 + ", " + argument2.ToString());
 
                 // sort through each objects with the tag "space"
                 for (var i = 0; i < characterSpaceOptions.Length; i++)
                 {
                     // get the currently selected objects position
                     Vector3 nextVect = characterSpaceOptions[i].transform.position;
 
                     // add the curerrent objects vector 3 to the array at the same position
                     characterSpacePostion[i] = new Vector3(nextVect.x, nextVect.y, nextVect.z);
                 }
         **/
 
         // get the red sphere that is supposed to go to each location in order
         GameObject selectionSphere = GameObject.Find("selectionSphere");
 
         // the button press is the index of the vector3 we need from the vector3 array
         // because it starts at 0 and adds one for every button press as seen below.
         // this puts the vector3 value at index (buttonpress) into a varable to use here.
         Vector3 positionToMoveTo = (characterSpacePostion[buttonPress]);
 
         // this is supposed to put the selection sphere at the location of the vector3
         // in the array at index (buttonpress) but it seems to be placing it at 0.0f, 0.0f, 0.0f?
         selectionSphere.transform.Translate(positionToMoveTo);
 
         // add one to button press to get the next vector3 out of the array using this as
         // an index.
         buttonPress++;
 
         // I only have 30 positions saved under the tag "space" at the moment
         // so this "should" allow it to loop back through once it's on the last one.
         if (buttonPress >= 30)
         {
             buttonPress = 0;
         }
     }
   //void myAnotherFunctionForOnClickEvent(string argument1, int argument2)
     //{
         // your code goes here
     //    print(argument1 + ", " + argument2.ToString());
     //}
 }
 
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 JoshDangIt · Sep 14, 2016 at 06:39 PM 0
Share

I don't have time to code an example of something like this. However, I did find this tutorial that may give you an idea.

https://www.youtube.com/watch?v=CzImJk7ZesI

0 Replies

· Add your reply
  • Sort: 

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

Instantiated object isnt moving 1 Answer

Problem with a motion script 0 Answers

Sum Of Two Motions? 0 Answers

Check if 2 objects positions are close enough 2 Answers

Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer


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