Instantiated Object has proper integer value after functions were called but returns 0 when referenced.

I honestly don’t know if the topic of this is actually correct. Anyway, to make it clearer, I have BulimawClass class as the base script of the BlackSagoScript script that’s attached to a Prefab.

This is a snippet of the BulimawClass:

...

//bulimaw level
	[SerializeField]
	private int level = 0;
	public void setLevel(int x) { level = x; }
	public int getLevel() { return level; }

	//bulimaw minimum level for bulimaws
	[SerializeField][HideInInspector]
	private int minLevel;
	public void setMinLevel(int x) { minLevel = x; }
	public int getMinLevel() { return minLevel; }

	//bulimaw maximum level for bulimaws
	[SerializeField][HideInInspector]
	private int maxLevel;
	public void setMaxLevel(int x) { maxLevel = x; }
	public int getMaxLevel() { return maxLevel; }

	//function to randomize level of bulimaws
	public void randomizeLevel(){
		setLevel (UnityEngine.Random.Range (getMinLevel (), getMaxLevel ()));
	}

...

And here’s a snippet of the BlackSagoScript attached to the Prefab that I’m going to instantiate:

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

public class BlackSagoScript : MonoBehaviour {


	public BulimawClass blackSago;
	public BulimawList bulimawList;
	public SkillsList skillsList;
	private SkillClass addSkill = new SkillClass ();

	void Start () {
		setWildData ();
		blackSago.randomizeLevel ();
		blackSago.calculateInitialAccumulatedExp ();
		blackSago.setRandomGender ();
		blackSago.calculatePhysique ();
		blackSago.randomizeBase ();
		blackSago.initialStatDistribution ();
		fillSkillPool ();
		randomizeSkills ();
	}

...

So, I instantiate it on this script:

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

[System.Serializable]
public class DataLoader : MonoBehaviour {

	public GameObject BlackSago;

	void Start () {
		GameObject bSago = (GameObject)Instantiate (BlackSago);
		bSago.GetComponent<BlackSagoScript> ().blackSago.setMinLevel (5);
		bSago.GetComponent<BlackSagoScript> ().blackSago.setMaxLevel (10);

		print (bSago.GetComponent<BlackSagoScript> ().blackSago.getLevel ());
	}

}

When I look at the instantiated object, I see this in the inspector:
77760-capture.png

But when I try to print the level of the instantiated object from the DataLoader script it returns 0. Everything else returns zero, actually. The only thing that’s actually correct is the minLevel and maxLevel. I’m desperate now, lol.

You are setting the level only in the Start() method which will not have been executed at the time you are printing the level.

I think if you move your initialization to the Awake() method, your values will be initialized during the Instantiate() call.

Also see here: http://forum.unity3d.com/threads/instantiate-prefabs-and-start-awake-functions.197811/