Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting and changing the position of an object in an array upon input

Discussion in 'Scripting' started by jjamerson018, Jul 27, 2021.

  1. jjamerson018

    jjamerson018

    Joined:
    Jul 22, 2021
    Posts:
    6
    I don't have any sample code right now because I haven't gotten a chance to start the project yet, but how would I do as the title asks? I'm thinking up a project but I'm kinda new to coding.

    Would it go something like this?:

    Code (CSharp):
    1. public class Example : MonoBehaviour
    2. {
    3.    
    4.     public GameObject[] clouds;
    5.  
    6.     //Say there's 3 elements in the array
    7.    
    8.     void update()
    9.     {
    10.         private GameObject cloud1 = clouds[0]; //  <-- btw.. can I do this?
    11.        
    12.         private Vector3 coord1 = new Vector3(5, 15, 45);
    13.         private Vector3 coord2 = new Vector3(-5, -15, -45);
    14.  
    15.         if(Input.GetKeyDown(KeyCode.Space))
    16.         {
    17.             cloud1.transform.position = coord1;
    18.         }
    19.         else if(Input.GetKeyDown(KeyCode.U))
    20.         {
    21.             cloud1.transform.position = coord2;
    22.         }
    23.     }
    24. }
    Thanks for checking out this thread :)
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,644
    Close! But this wouldn't run! Here's what your code would look like if you wanted it to run correctly:
    Code (CSharp):
    1. public class Example : MonoBehaviour
    2. {
    3.  
    4.     public GameObject[] clouds;
    5.    
    6.     private Vector3 coord1 = new Vector3(5, 15, 45); // <== you don't need to initialize these every update
    7.     private Vector3 coord2 = new Vector3(-5, -15, -45);
    8.  
    9.     void Update() // <== Update needs to be capitalized for it to get called by Unity
    10.     {
    11.         GameObject cloud1 = clouds[0]; //  <== you can't make things inside of a method private, only in the class definition
    12.  
    13.         if(Input.GetKeyDown(KeyCode.Space))
    14.         {
    15.             cloud1.transform.position = coord1;
    16.         }
    17.         else if(Input.GetKeyDown(KeyCode.U))
    18.         {
    19.             cloud1.transform.position = coord2;
    20.         }
    21.     }
    22. }
    There's definitely some other things I would do differently, but here's a starter.
     
  3. jjamerson018

    jjamerson018

    Joined:
    Jul 22, 2021
    Posts:
    6
    Thank you, yeah I know the basic parts of the code is pretty scrappy (such as my uncapitalized update lol) its something I just threw together for the example. Would "cloud1.transform.position" work? I can't think of how I would reposition a specific object out of an array.

    Thanks for your time!
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,644
    I don't see why it wouldn't work... have you tried running this in a test project?