Creating an object using a prefab, with parent, text etc..
So my question is how do i properly use an prefab because i just can't quite figure it out, in my tests i have been getting the following:
Here is my code that is on the Button that i will use OnClick() after i fix this:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class DrawDeck : MonoBehaviour
{
public Transform Card;
// Start is called before the first frame update
void Start()
{
Object prefab = AssetDatabase.LoadAssetAtPath("Assets/Card.prefab", typeof(GameObject));
GameObject clone = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
clone.transform.position = Vector3.one;
}
// Update is called once per frame
void Update()
{
}
}
When i check on the Object expanding him i see that i got everything from the Card prefab to the Card(Clone) (exactly what it should be).
However i dont know how to set a parent that i dont have anything on(in this case would be PlayerHand disregarding the Card that already is there), or even edit the CardImage, CardTitle,CardDesc etc.
When i create a new Card can i set the CardTitle in a script or in this case on this script?
How do tell unity what parent he should put the card? And why is this Card(Clone) invisible?
Any Help would be appreciated, ty :)
Answer by xxmariofer · May 07, 2019 at 08:30 AM
first question yuou can have the parent saved in a variable drag and dropped from the editor for example, also i imagine the tittle is a text si you will need to add the unityengine.ui namespace at the top. the card should be visible once is parented in the cavas
public Transform Card; // Start is called before the first frame update Transform parent;
void Start()
{
Object prefab = AssetDatabase.LoadAssetAtPath("Assets/Card.prefab", typeof(GameObject));
GameObject clone = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
clone.transform.SetParent(parent);
clone.transform.Find("CardTitle ").GetComponent<Text>().text = "yourNewTittle";
clone.transform.position = Vector3.one;
}
the
Your answer
Follow this Question
Related Questions
order of scripts 2 Answers
Assignement object 1 Answer
The laser problems 0 Answers
Is it possible to make a script that pushes this button? 1 Answer
Adda cooldown to a Shooting Script 0 Answers