- Home /
Question by
TheGeorg · May 12, 2019 at 04:04 PM ·
transform.positionbuttonsui image
image prefab transform does not work
I dont know why it does not work. It should set the prefab position to the button position
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SetLocation : MonoBehaviour { public GameObject[] Metal; public Transform SpawnPoint; private Transform WhichButton; public GameObject canvas; public GameObject WhichMetal; private int RandomInt;
private void Start()
{
if (RandomInt == 1)
{
WhichMetal = Metal[0];
}
else if (RandomInt == 2)
{
WhichMetal = Metal[1];
}
else
{
WhichMetal = Metal[2];
}
GameObject ParentMetal = Instantiate(WhichMetal, SpawnPoint.position, Quaternion.identity) as GameObject;
ParentMetal.transform.SetParent(canvas.transform);
}
//here i call the function with one of my buttons
public void Location(Transform ButtonTransform)
{
WhichButton = ButtonTransform;
WhichMetal.transform.position = WhichButton.position;
WhichMetal.transform.localScale = WhichButton.localScale;
RandomInt = Random.Range(1, 4);
if (RandomInt == 1)
{
WhichMetal = Metal[0];
} else if (RandomInt == 2)
{
WhichMetal = Metal[1];
} else
{
WhichMetal = Metal[2];
}
GameObject ParentMetal = Instantiate(WhichMetal, SpawnPoint.position, Quaternion.identity) as GameObject;
ParentMetal.transform.SetParent(canvas.transform);
}
//only for testing
private void Update()
{
WhichMetal.transform.position = WhichButton.position;
WhichMetal.transform.localScale = WhichButton.localScale;
}
}
Comment
you want to change the parent metal positiom (the instantiated object) pr the object thats set as prefab? you need to access the recttransform not the transform. (all canvas elements use the rectransform for positioning)