- Home /
Start() is called 2 times when Instantiating prefab
Hi all,
I got problems with my simple 2D matching game. Here's the class, which instantiates my Objects:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public int life = 3;
public int points = 0;
// Use this for initialization
void Start () {
GameObject check1 = (GameObject)Instantiate(Resources.Load("Checkin"));
Checkin ch = check1.GetComponent<Checkin>();
ch.type = PType.russe;
check1.transform.position = new Vector3 (-6f, 2.5f, 0f);
GameObject check2 = (GameObject)Instantiate(Resources.Load("Checkin"));
Checkin ch2 = check2.GetComponent<Checkin>();
ch2.type = PType.franzose;
check2.transform.position = new Vector3 (0f, 2.5f, 0f);
GameObject check3 = (GameObject)Instantiate(Resources.Load("Checkin"));
Checkin ch3 = check3.GetComponent<Checkin> ();
ch3.type = PType.chinese;
check3.transform.position = new Vector3 (6f, 2.5f, 0f);
GameObject ps = (GameObject)Instantiate(Resources.Load("Passenger"));
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUI.Box(new Rect (10,10,100,20),"Points: " + points.ToString());
GUI.Box(new Rect (10,40,100,20),"Lifes: " + life.ToString());
}
}
public enum PType {
russe,
franzose,
chinese,
none
}
And the Passengerclass,where I think the problem occueres:
using UnityEngine;
using System.Collections;
public class Passenger : MonoBehaviour {
int count = 0;
PType type;
public bool alive = true;
readonly int rand = Random.Range(0, 3);
// Use this for initialization
void Start () {
if (rand == 0)
{
type = PType.franzose;
renderer.material = (Material)Resources.Load("Materials/franzose");
}
else if (rand == 1)
{
type = PType.russe;
renderer.material = (Material)Resources.Load("Materials/russe");
}
else
{
type = PType.chinese;
renderer.material = (Material)Resources.Load("Materials/chinese");
}
count++;
print ("Pessenger is " + type.ToString () + " with count " + count + " " + Time.time);
}
The Problem is, that the Passenger Script is called 2 times and so I get 2 random Values for one Object, which screws up the whole matching. I checked with printouts that the script assignes the Passenger gameObject two times (but there's only one gameObjectin the Hierarchy).I appreciate every Help :D
Thanks, Gian-Luca
Answer by _dns_ · Nov 29, 2014 at 06:47 PM
Hi, you may have 2 "passenger" objects in your scene, then Start is called on each of them. One may already be in your scene when you start the game, or maybe you have 2 "controler" objects that create 2 "passenger".
To identify an instance of an object, print the GetInstanceID() on the GameObject or the Component, it will give you an ID that is unique for each object of your scene. This will help you find out if there are 2 objects. This ID is also visible in the editor, just right click on the "tab" part of an Inspector window and choose "debug" in the popup menu : there will be additional information to check.
You could also set a breakpoint on the part of the code you want to inspect and use the debugger feature of monodevelop+unity.
Thanks for your answer! I've run the game in edit mode again and got some errors saying that I can generate the random value only in the mainthread. So I packed the random generation in the controller class. This solved the problem but I don't know if it was the source of the problem.