Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question