- Home /
Changing a variable in the inspector while game is running doesn't work,When changing variable in inspector while game running, program stops responding
I am following the Junior Programmer tutorial Prototype 2.3 - Step 3 in Unity Learn, using Unity 2018.4.29f I have exposed the public int animalIndex to the inspector. The value can be 0, 1, or 2, depending on the animal I want to instantiate by pressing the "S" key. If I enter the number in the inspector before the game starts, everything works fine. If I try to change the number while the game is running, it will not instantiate anything when I press the "S" key. It's as if the "S" key no longer works. No error messages are generated. The GameObject Array animalPrefabs is populated in the inspector window with the animal prefabs. I've been developing games in Unity for 2 years now and every now and then something stops working, depending on the version. (I have not had this problem before).I am using Visual Studio 2019, latest version.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public GameObject[] animalPrefabs;
public int animalIndex;
void Update()
{
if(Input.GetKeyDown(KeyCode.S))
{
Instantiate(animalPrefabs[animalIndex], new Vector3(0, 0, 20), animalPrefabs[animalIndex].transform.rotation);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613021035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity how to validate/preprocess variables when they're assigned in the inspector 1 Answer
Warning with Unity 2018.1.3 and Visual Studio 2017 1 Answer
Unity 2017 not giving option for latest target API level 26 or higher. 3 Answers
Networking - Unity stuck by pressing play button in editor 2018 2 1 Answer
Need help with enum and stuff (right, left click) in JavaScript 0 Answers