Author Topic: Object Instantiation Problem  (Read 7407 times)

TwistyD

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Object Instantiation Problem
« on: April 29, 2014, 04:49:55 AM »
Hello,

I'm new here to these forums I'll introduce myself properly later, but first I need urgent help with a problem.

You see there are three parts to my little problem



I'm trying to create a "bullet" that fires up from the where the firing mechanism object is located. The firing mechanism is a child of the player object (the red arrow) and thus moves with the object's position. When looking at the mechanism's properties in the inspector it always stays at the same position:



The way I have it set up is that I'm trying to instantiate a loaded prefab into the scene and set it's position to the position of the firing mechanism. When I instantiate the object how ever it spawns at a different spot that the mechanism:

For example the game starts with the player object always at this position

(-1.0,-0.5,0.0)

When I instantiate a blocks they appear in different positions:

Block 1: (-1.0,-0.5,0.0)
Block 2: (-1.0,-0.5,0.0)
Block 3: (-6.0,-0.5,0.0)

Furthermore: These positions aren't reflected at all in the properties of the instantiated block objects.

Is there a way for me to set the blocks position when I call an object through NGUI or Unity? It seems like this would be easy but I'm genuinely having a hard time.

Thank you in advance!

I've included the problematic code:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class FiringMechanism : MonoBehaviour
  6. {
  7.  
  8.     /*
  9.      * The purpose of this script is to instantiate blocks from the position of the gameobject "Firing Mechanism".
  10.      * It is ENTIRELY possible to do this from the player script but due to NGUI's strange problem with positioning (of my lack of coding prowess),
  11.      * it's much better to have a seperate script handle the firing of blocks.
  12.      */
  13.  
  14.     public List<GameObject> playerBlocks; //The blocks that the player can shoot.
  15.     public GameObject currentBlock;
  16.  
  17.     private GameObject playerObject;
  18.  
  19.     private GameObject gameController; //the game controller object
  20.     private GameObject firingMechanismObject; // self reference
  21.     private GameObject gamePanel; // the game panel object
  22.  
  23.     private GameObject redBlock;
  24.     private GameObject blueBlock;
  25.     private GameObject greenBlock;
  26.     private GameObject yellowBlock;
  27.     private GameObject purpleBlock;
  28.     private GameObject blackBlock;
  29.     private GameObject whiteBlock;
  30.     private GameObject cyanBlock;
  31.  
  32.  
  33.  
  34.  
  35.  
  36.     void Awake()
  37.     {
  38.         firingMechanismObject = GameObject.Find("FiringMechanism");
  39.         playerObject = GameObject.Find("Player");
  40.         gamePanel = GameObject.Find("GamePanel");
  41.  
  42.         currentBlock = new GameObject();
  43.         currentBlock.AddComponent<Rigidbody2D>();
  44.         currentBlock.AddComponent<BoxCollider2D>();
  45.  
  46.  
  47.     }
  48.  
  49.         // Use this for initialization
  50.         void Start ()
  51.     {
  52.         InitFireList();
  53.         }
  54.        
  55.         // Update is called once per frame
  56.         void Update ()
  57.     {
  58.         Debug.Log(firingMechanismObject.transform.position);
  59.         }
  60.  
  61.     void InitFireList()
  62.     {
  63.         //Load the block prefabs
  64.         redBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireRedBlock") as GameObject;
  65.         greenBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireGreenBlock") as GameObject;
  66.         blueBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireBlueBlock") as GameObject;
  67.         yellowBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireYellowBlock") as GameObject;
  68.         purpleBlock = Resources.Load("GameObjects/PlayerFireBlocks/FirePurpleBlock") as GameObject;
  69.         blackBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireBlackBlock") as GameObject;
  70.         whiteBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireWhiteBlock") as GameObject;
  71.         cyanBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireCyanBlock") as GameObject;
  72.  
  73.         //add the loaded blocks to the playerBlock list.
  74.         playerBlocks.Add(redBlock);
  75.         playerBlocks.Add(blueBlock);
  76.         playerBlocks.Add(greenBlock);
  77.         playerBlocks.Add(yellowBlock);
  78.         playerBlocks.Add(purpleBlock);
  79.         playerBlocks.Add(blackBlock);
  80.         playerBlocks.Add(whiteBlock);
  81.         playerBlocks.Add(cyanBlock);
  82.         currentBlock = playerBlocks [Random.Range(0, playerBlocks.Count)]; //Changes the currently selected block to a random element inside the Blocks array
  83.     }
  84.  
  85.     public void FireBlock()
  86.     {
  87.         //Fires the currentBlock upward from the Firing Mechanism.
  88.         //GameObject block = (GameObject) Instantiate(currentBlock, playerObject.transform.position, playerObject.transform.rotation);
  89.      
  90.         NGUITools.AddChild(gamePanel, currentBlock);
  91.  
  92.         currentBlock.transform.position = firingMechanismObject.transform.position;
  93.         currentBlock.rigidbody2D.AddForce(Vector2.up);
  94.         currentBlock = playerBlocks [Random.Range(0, playerBlocks.Count)]; //Changes the currently selected block to a random element inside the Blocks array
  95.         Debug.Log(currentBlock.transform.position);
  96.  
  97.     }
  98. }

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Object Instantiation Problem
« Reply #1 on: April 29, 2014, 05:29:04 AM »
Inspector shows localPosition and you're setting and printing out Global position. Use .localPosition instead.

TwistyD

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Object Instantiation Problem
« Reply #2 on: April 29, 2014, 05:47:24 AM »
Thanks for your response...I tried setting the object to the local position but it doesn't have an effect.

Printing the local position in the console window shows the same sporadic output for the Firing Block Position.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Object Instantiation Problem
« Reply #3 on: April 29, 2014, 07:19:30 AM »
Ok taking a step back, there's a bunch of issues in your code.

CurrentBlock is created in Awake as an empty gameobject, with rigidbody and 2dbox collider added - with no sizes at all.

You load all the playerblocks, but never instantiate them - that means that your PlayerBlock list is full of prefab references, instead of their own instantiated objects.

Then you override the CurrentBlock with a random prefab from the list, making the original CurrentBlock redundant. This removes the rigidbody and boxcollider2d, unless the prefabs have their own. The prefab overrides it entirely.

  1.  NGUITools.AddChild(gamePanel, currentBlock);

since currentblock is a prefab, this is fine - instantiates the currentblock to a new gameobject, but you're not using the GameObject that's returned, instead you're settings and reading values directly from a prefab.

You are also never initializing the list of playerblocks, which should make it crash at compile time, but I assume it's being set from inspector since it's public.


Try something like this

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class FiringMechanism : MonoBehaviour
  6. {
  7.  
  8.     /*
  9.      * The purpose of this script is to instantiate blocks from the position of the gameobject "Firing Mechanism".
  10.      * It is ENTIRELY possible to do this from the player script but due to NGUI's strange problem with positioning (of my lack of coding prowess),
  11.      * it's much better to have a seperate script handle the firing of blocks.
  12.      */
  13.  
  14.     public List<GameObject> playerBlockPrefabs = new List<GameObject>(); //The blocks that the player can shoot.
  15.     public GameObject currentBlockPrefab = null;
  16.  
  17.     private GameObject playerObject;
  18.  
  19.     private GameObject gameController; //the game controller object
  20.     private GameObject firingMechanismObject; // self reference
  21.     private GameObject gamePanel; // the game panel object
  22.  
  23.     private GameObject redBlock;
  24.     private GameObject blueBlock;
  25.     private GameObject greenBlock;
  26.     private GameObject yellowBlock;
  27.     private GameObject purpleBlock;
  28.     private GameObject blackBlock;
  29.     private GameObject whiteBlock;
  30.     private GameObject cyanBlock;
  31.  
  32.     void Awake()
  33.     {
  34.         firingMechanismObject = GameObject.Find("FiringMechanism");
  35.         playerObject = GameObject.Find("Player");
  36.         gamePanel = GameObject.Find("GamePanel");
  37.     }
  38.  
  39.     // Use this for initialization
  40.     void Start ()
  41.     {
  42.         InitFireList();
  43.     }
  44.    
  45.     // Update is called once per frame
  46.     void Update ()
  47.     {
  48.         Debug.Log(firingMechanismObject.transform.position);
  49.     }
  50.  
  51.     void InitFireList()
  52.     {
  53.         //Load the block prefabs
  54.         redBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireRedBlock") as GameObject;
  55.         greenBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireGreenBlock") as GameObject;
  56.         blueBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireBlueBlock") as GameObject;
  57.         yellowBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireYellowBlock") as GameObject;
  58.         purpleBlock = Resources.Load("GameObjects/PlayerFireBlocks/FirePurpleBlock") as GameObject;
  59.         blackBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireBlackBlock") as GameObject;
  60.         whiteBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireWhiteBlock") as GameObject;
  61.         cyanBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireCyanBlock") as GameObject;
  62.  
  63.         //add the loaded blocks to the playerBlock list.
  64.         playerBlockPrefabs.Add(redBlock);
  65.         playerBlockPrefabs.Add(blueBlock);
  66.         playerBlockPrefabs.Add(greenBlock);
  67.         playerBlockPrefabs.Add(yellowBlock);
  68.         playerBlockPrefabs.Add(purpleBlock);
  69.         playerBlockPrefabs.Add(blackBlock);
  70.         playerBlockPrefabs.Add(whiteBlock);
  71.         playerBlockPrefabs.Add(cyanBlock);
  72.         currentBlockPrefab = playerBlockPrefabs [Random.Range(0, playerBlockPrefabs.Count)]; //Changes the currently selected block to a random element inside the Blocks array
  73.     }
  74.  
  75.     public void FireBlock()
  76.     {
  77.         //Fires the currentBlockPrefab upward from the Firing Mechanism.        
  78.      
  79.         var myFiringBlock = NGUITools.AddChild(gamePanel, currentBlockPrefab);
  80.  
  81.         myFiringBlock.transform.position = firingMechanismObject.transform.position;
  82.         //add rigidbody if you don't have them on the prefab.
  83.         myFiringBlock.rigidbody2D.AddForce(Vector2.up);
  84.         Debug.Log(myFiringBlock.transform.position);
  85.  
  86.         currentBlockPrefab = playerBlockPrefabs [Random.Range(0, playerBlockPrefabs.Count)]; //Changes the currently selected block to a random element inside the Blocks array
  87.        
  88.  
  89.     }
  90. }

TwistyD

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Object Instantiation Problem
« Reply #4 on: April 29, 2014, 03:34:44 PM »
Alright I tried it.

The error I got was an argument exception. I think it might have something to do with the currentBlockPrefab object being set as null.

Heres the error it gives me

  1. ArgumentException: The thing you want to instantiate is null.
  2. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:104)
  3. UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:90)
  4. NGUITools.AddChild (UnityEngine.GameObject parent, UnityEngine.GameObject prefab) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:403)
  5. FiringMechanism.FireBlock () (at Assets/FiringMechanism.cs:80)
  6. PlayerScript.PlayerMovement () (at Assets/Scripts/PlayerScript.cs:65)
  7. PlayerScript.Update () (at Assets/Scripts/PlayerScript.cs:47)

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Object Instantiation Problem
« Reply #5 on: April 30, 2014, 02:27:26 AM »
Try to debug.log both gamepanel and currentblockprefab before the AddChild to make sure they are not null. It may be that the random selector hasn't chosen any of them.

TwistyD

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Object Instantiation Problem
« Reply #6 on: April 30, 2014, 02:55:50 AM »
Talk about a Deus ex Machina...The problem seems to have resolved itself...I'll look into how later. The script is working excellently! I'm new to programming in Unity and NGUI by extension so I'm not use to working with prefabs and the like yet.

Thank you SO MUCH for your help!