- Home /
Code being run only when object is selected in editor.
Hello all,
I'm trying to have a simply level progression system where after the player defeats the enemy, they move up a level. I tried making it so one object references a variable that doesn't go away due to DontDestroyOnLoad(). However, it seems like this code is only run when I have the main camera selected in the editor. I don't think its a code issue because I don't have anything to do with the editor in my code, but to be honest this doesn't make any sense to me as to why this is a thing.
What I have attached to the camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Loader : MonoBehaviour {
public GameObject gameMananger;
private BoardManager boardManager;
public BoardManager BM;
void Awake() {
if (GameManager.instance == null)
Instantiate(gameMananger);
else {
boardManager = gameMananger.GetComponent<BoardManager>();
boardManager.SetupScene();
}
BM = gameMananger.GetComponent<BoardManager>();
//This usually stays at 1 unless I click on the camera
//When I have the camera selected it displays both the Log and increments BM.Level
//When I either have nothing selected or anything but the camera selected it only displays the Log but resets back to level 1
Debug.Log("Adding to level");
BM.Level++;
}
}
And just for reference, here is the boardManager that contains "Level":
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
public class BoardManager : MonoBehaviour {
public static BoardManager instance = null;
[Serializable]
public class Count {
public int minimum;
public int maximum;
public Count(int min, int max) {
minimum = min;
maximum = max;
}
}
public int columns = 2;
public int rows = 2;
private int level = 1;
public int Level{
get {
return level;
}
set {
level = value;
}
}
public GameObject boardTile;
public GameObject[] visitedTiles;
private Transform boardHolder;
private List<Vector3> gridPositions = new List<Vector3>();
public GameManager gameManager;
public void InitializeList() {
gridPositions.Clear();
for(int x = 1; x < columns-1; x++) {
for(int y = 1; y < rows-1; y++) {
gridPositions.Add(new Vector3(x, y, 0f));
}
}
}
public void BoardSetup() {
gameManager = gameObject.GetComponent<GameManager>();
Debug.Log("Level " +level);
boardHolder = new GameObject("Board").transform;
int count = 0;
for (int x = -1; x < columns; x++) {
for(int y=-1; y < rows; y++) {
GameObject instance = Instantiate(boardTile, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;
instance.transform.SetParent(boardHolder);
instance.name = (++count).ToString();
//If at current level
if (level == count)
instance.GetComponent<SpriteRenderer>().color = new Color(0f, 1f, 0f, 1f);
//If at previous level
else if (level > count)
instance.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 0f, 1f);
}
}
}
public void SetupScene() {
BoardSetup();
InitializeList();
}
}
If I need to provide any other information, please let me know because I cannot think of a reason as to why this is happening. I don't think any of my code has anything to do with me having the camera selected.
Your answer
![](https://koobas.hobune.stream/wayback/20220612104040im_/https://answers.unity.com/themes/thub/images/avi.jpg)