A
A
Alex Blinov2019-03-22 08:55:38
Game development
Alex Blinov, 2019-03-22 08:55:38

How to change the code so that it fulfills certain requirements?

There is a code for the unit. how to make it so that it can be tied to a button and each time it is pressed, iterates over the elements in the array. For example: Before pressing when entering the game: car BUS motorcycle. After pushing car bus MOTORCYCLE and so on.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Script : MonoBehavior {
public GameObject[] objects;
void Start () {
inst_obj = Instantiate (objects[1], objects[1].transform.position, Quaternion.identity);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mopsicus, 2019-03-22
@mopsicus

Mmm, make a variable with an index and change it every time?
Something like that, didn't check

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Script : MonoBehaviour {

public GameObject[] objects;
private int _index = 1;

void Start () {
    CreateObj();
}

void CreateObj(){
    inst_obj = Instantiate (objects[_index], objects[_index].transform.position, Quaternion.identity);    
}

void Update() {
    if(Input.GetMouseDown(0){
       _index++;
       if (_index == objects.Length) {
           _index = 0;
       }
       CreateObj();
    }    
}

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question