Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gian-Luca · Nov 29, 2014 at 02:31 PM · 2dinstantiateprefabstart

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gian-Luca · Nov 29, 2014 at 07:19 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

The prefab you want to instantiate is null. 2 Answers

Add force to instantiated prefabs (c#) 0 Answers

Start() of Instantiated Prefabs Not Being Carried Out? 1 Answer

Trouble destroying a instantiated Prefab? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges