-1
\$\begingroup\$

I am trying to translate a prefab of a puzzle piece made of a hierarchy of cubes but it's not working.

I have tried translating the GameObject using its transform and the function Translate, it did not move. And I have tried translating each child of the GameObject's transform as well and it did not move either. Looking at the transform position's value in the debugger showed that it did change however. Also "activeinHierarchy" is set to false and I am wondering if it could be the problem.

Here is the code for the second method:

enum NanoPieceType
{
    IPIECE,
    TPIECE,
    LPIECE,
    OPIECE
}

public class GameController : MonoBehaviour
{
    public GameObject INanoPiece, TNanoPiece, LNanoPiece, ONanoPiece;
    private NanoPieceType currentPiece;
    private int speed;

    void Start()
    {
        currentPiece = (NanoPieceType)Random.Range(0, 3);
        speed = 1;
        CreateNanoPiece((NanoPieceType)currentPiece, new Vector3(0f, 10f, 0f), 
   new Quaternion(0f, 0f, 0f, 0f));

   }

   // Update is called once per frame
   void Update()
   {

        switch (currentPiece)
        {
            case NanoPieceType.IPIECE:
                MovePieceDown(INanoPiece);
                break;
            case NanoPieceType.LPIECE:
                MovePieceDown(LNanoPiece);
                break;
            case NanoPieceType.OPIECE:
                MovePieceDown(ONanoPiece);
                break;
            case NanoPieceType.TPIECE:
                MovePieceDown(TNanoPiece);
                break;

            default:
                break;
        }


   }

   void MovePieceDown(GameObject obj)
   {
        GameObject child = obj;

        child.transform.Translate(0f, -1.0f*Time.deltaTime, 0f);

        for (int i = 0; i < child.transform.childCount;i++)
        {
            GameObject newObj = child.transform.GetChild(i).gameObject;
            newObj.transform.Translate(0f, -1.0f * Time.deltaTime, 0f);
        }

    }
}

void CreateNanoPiece(NanoPieceType type, Vector3 pos, Quaternion rot)
{
    switch (type)
    {
        case NanoPieceType.IPIECE:
            Instantiate(INanoPiece, pos, rot);
            break;
        case NanoPieceType.LPIECE:
            Instantiate(LNanoPiece, pos, rot);
            break;
        case NanoPieceType.OPIECE:
            Instantiate(ONanoPiece, pos, rot);
            break;
        case NanoPieceType.TPIECE:
            Instantiate(TNanoPiece, pos, rot);
            break;

        default:
            break;
        }
    }
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I'm sorry I just found that I had to attach a movement/translation script to the prefab, and then instantiate it in another script. I did this and it works.

I come from an UE4 background so this philosophy is entirely new to me.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .