Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

spawn 4 prefabs on start

Discussion in 'Scripting' started by Northrop, May 26, 2015.

  1. Northrop

    Northrop

    Joined:
    May 22, 2015
    Posts:
    14
    Hi,
    I know there are few entries on that topic, but non seems to work for me.

    I want all my 4 characters to spawn at game play. Until the moment I:
    - defined the variables
    Code (JavaScript):
    1.  
    2. var girl1 : GameObject;
    3. var spawnpoint1: Transform;
    4.  
    and run under
    Code (JavaScript):
    1.  
    2. function Start () {
    3. Instantiate(girl1, spawnpoint1.position, girl1.rotation);
    4. }
    5.  
    any ideas?
    From console:

    UnassignedReferenceException: The variable a of NetworkConnection has not been assigned.
    You probably need to assign the a variable of the NetworkConnection script in the inspector.
    UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransform.gen.cs:28)
    NetworkConnection.Start () (at Assets/Scripts/NetworkConnection.js:51)[/code]
     
    Last edited: May 26, 2015
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    For starters, use [ code] code tags [/code], it makes pasted code a lot easier to read.

    That error is clearly unrelated to the code you've posted - there's an undefined variable called "a" in the NetworkConnection.js script at line 51.
     
  3. Northrop

    Northrop

    Joined:
    May 22, 2015
    Posts:
    14
    Thanks, I will do

    I remove the "a" variable now. And started again. Defined the variables and use the above order.
    But nothings happens.


    Code (JavaScript):
    1.  
    2. //Variables for spawing prefab
    3. var girl1: GameObject;
    4.  
    5. //Variables for spawnpoints
    6. var spawnpoint1: Transform;
    7. var spawnpoint2: Transform;
    8.  
    9.  
    Code (JavaScript):
    1.  
    2.  
    3. function SpawnPlayersStart()
    4.      {
    5.       Instantiate(girl1, transform.position, transform.rotation);
    6.  
    7.      }
    8.  
    the I tried with
    Code (JavaScript):
    1.  
    2. function SpawnPlayersStart()
    3.      {
    4.       Instantiate(girl1, spawnpoint1.transform.position, spawnpoint1.transform.rotation);
    5.  
    6.      }
    7.  
    8.  
     
    Last edited: May 26, 2015
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Well, you still only have one Instantiate call. So of course there's only one character spawned.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Also, when you say 'nothing happens', do you mean that it's also not spawning the first character either? If so, are you calling SpawnPlayersStart anywhere?
     
  6. Northrop

    Northrop

    Joined:
    May 22, 2015
    Posts:
    14
    I have only one order at the moment. If I get this one working, I will try with all four.
    With nothing happens, I mean the prefab does not spawn, it complies OK and console shows no error.

    I am not caling SpawnPlayersStart anywhere.... should I?
     
  7. Northrop

    Northrop

    Joined:
    May 22, 2015
    Posts:
    14
    sorry, yes, I am using
    Code (JavaScript):
    1. function SpawnPlayersStart()
    2.         {
    3.                 Instantiate(girl1, spawnpoint1.position, spawnpoint1.rotation);
    4.  
    5.         }
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    You have to call SpawnPlayersStart somewhere in order for that code to execute. In your first post, you had the code in Start(), which is called by the Unity engine, as long as the script is attached to an object. However, it does not do the same with functions that have your own names on them, like that one does.
     
  9. Northrop

    Northrop

    Joined:
    May 22, 2015
    Posts:
    14
    I'm sorry but I don' follow.
    Now I have this script (named SpawnPlayersStart) attached to one of the players prefabs
    Code (JavaScript):
    1. #pragma strict
    2. //Variables for spawing prefab
    3. var player1: GameObject;
    4. var player2: GameObject;
    5. var player3: GameObject;
    6. var player4: GameObject;
    7.  
    8.  
    9. //Variables for spawnpoints
    10. var point_1: Transform;
    11. var point_2: Transform;
    12. var point_3: Transform;
    13. var point_4: Transform;
    14.  
    15.  
    16. function Start ()
    17. {
    18.  
    19.  
    20.                 Instantiate(player1, point_1.position, point_1.rotation);
    21.                 Instantiate(player2, point_2.position, point_2.rotation);
    22.                 Instantiate(player3, point_3.position, point_4.rotation);
    23.                 Instantiate(player4, point_4.position, point_4.rotation);
    24.  
    25. }
    26.  
    27. function Update () {
    28.  
    29. }
    But that is not enough. From where exactly I should call SpawnPlayersStart?
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    You say it's attached to one of the player prefabs - it needs to be attached to an object that's in your scene in order to be run. (In fact, you definitely DON'T want it attached to the prefabs; if it is, then spawning that prefab will cause an infinite loop of spawning prefabs and spawning more prefabs because they spawn when they spawn.)