I am making a achievement system to save all my achievements when unlock and show which achievements is not.When I try instantiate a prefab, the z position is making my prefab look small and out of place. I need to set my position z to 0 using c# code. At the moment when I instantiate a prefab the z position value is 1557.057 and making the prefab look small in my panel.
code for instantiating the prefabs in my panel
using UnityEngine;
using System.Collections;
public class AchievementManager : MonoBehaviour {
public GameObject achievementPrefab;
// Use this for initialization
void Start ()
{
CreateAchievement("Streak");
CreateAchievement("Streak");
CreateAchievement("Streak");
}
public void CreateAchievement(string category)
{
GameObject achievement = (GameObject)Instantiate(achievementPrefab);
SetAchievementInfo(category, achievement);
}
public void SetAchievementInfo(string category , GameObject achievement)
{
achievement.transform.SetParent(GameObject.Find(category).transform);
achievement.transform.localScale = new Vector3(1, 1, 1);
}
}
Instantiate
can take position and rotation as second and third parameters respectively, so you can instantiate the object with world position and rotation of your category
object like this:
GameObject achievement = (GameObject)Instantiate(
achievementPrefab,
GameObject.Find(category).transform.position,
GameObject.Find(category).transform.rotation
);
Also, transform.SetParent()
can take second parameter worldPositionStays
, you can try setting it to false
achievement.transform.SetParent(GameObject.Find(category).transform, false);
Lastly, you can set the Z directly (the following resets only Z):
achievement.transform.localPosition = new Vector3(
achievement.transform.localPosition.x,
achievement.transform.localPosition.y,
0
);