- Home /
Question by
IbrahimAlkaber · Dec 21, 2019 at 10:42 AM ·
unity 5scripting problem
Switch vehicle
Can someone tell me where the problem with my code pls
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SwitchVeh : MonoBehaviour
{
private GameObject[] vehList;
private int index;
private void Start()
{
index = PlayerPrefs.GetInt("VehSelected");
vehList = new GameObject[transform.childCount];
for(int i=0; i<transform.childCount; i++)
vehList[i] = transform.GetChild(i).gameObject;
foreach(GameObject go in vehList)
go.SetActive(false);
if (vehList[index])
vehList[index].SetActive(true);
}
public GameObject[] Tab;
public void SelectTab(int buttonIndex)
{
for (int i = 0; i < Tab.Length; ++i)
Tab[i].SetActive(i == buttonIndex);
}
public void ConfirmButton()
{
PlayerPrefs.SetInt("VehSelected", index);
SceneManager.LoadScene("GameMenu");
}
}
Comment
Best Answer
Answer by Statement · Dec 21, 2019 at 12:17 PM
It appear that
index = PlayerPrefs.GetInt("VehSelected");
give an index outside the array vehLists bounds.
Try this:
index = PlayerPrefs.GetInt("VehSelected");
Debug.Log("VehSelected: " + index + " of " + transform.childCount);
to check if your PlayerPrefs VehSelected is outside bounds of transforms child count.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220451im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting a mix of warnings in multiplayer 0 Answers
Point Counter Works Only Once! 1 Answer
How to make an enemy ragdoll on death? 1 Answer