Hello Guest

Author Topic: Instantiate into ANCHOR camera?  (Read 4168 times)

DemiGoth

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Instantiate into ANCHOR camera?
« on: November 13, 2013, 06:48:20 pm »
For my upcoming (heavily modified) Tetris game I need to break down the dropping blocks into bits. I figured to make a prefab of the colored (1x1) blocks, drop one on the screen (BEHIND the camera reach) and then use Instantiate to clone it. For the easy of testing I'm using the press of N to create it on a hard-coded spot.
Code: [Select]
using UnityEngine;
using System.Collections;

public class TetrisBits : MonoBehaviour
{
public Transform myObject;

// Use this for initialization
void Start ()
{
}

// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.N) && Input.GetKeyDown(KeyCode.N))
{
Instantiate(myObject, new Vector3(176,261,1),myObject.rotation);
}
}
}
Well, code works, but it's not created on (176,261,1) in the anchor where the prefab sprite resides, but instead it's on those coordinates in the MAIN camera (and thus waaay off where I want it :(). Now I'm wondering how I can tell the object to appear under the anchor it's parent is instead of the clone popping up on the main window. Also worth to note that myObject.rotation is not working as 'anchor' as the tutorial on the Unity site tells me.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Instantiate into ANCHOR camera?
« Reply #1 on: November 13, 2013, 10:26:39 pm »
Once you instantiate you will have to reparent.
GameObject go = Instantiate(myObject) as GameObject;
go.transform.parent = myObject.parent;
and then set go.transform.localPosition.

DemiGoth

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Instantiate into ANCHOR camera?
« Reply #2 on: November 14, 2013, 06:10:44 am »
I suppose that go is initiated like:

public GameObject go;

and then drag & drop the anchor camera into it?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Instantiate into ANCHOR camera?
« Reply #3 on: November 14, 2013, 02:05:02 pm »
Its the other way, you want to parent this instantiated game object to the anchor...