I need something that I can't describe, bet its simple for you

So, when I press a button it creates a Player gameobject and Score text, but I can’t modify score text because it is in prefab folder, but i want to make it to be in my scene. Please help?

Video to easier understand

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreatePlayerInBegining : MonoBehaviour
{
    public GameObject Player;
    public Transform canvas;
    public Text ScoreText;
    public Text FinalText;
    public Button button;
    public GameObject ButtonObject;

    void Start ()
    {
        button = GetComponent<Button>();
        button.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        Vector3 playerSpawnPosition = new Vector3(0, -4, 0);
        Instantiate(Player, playerSpawnPosition, Quaternion.identity);
        Vector3 scoreTextSpawnPosition = new Vector3(0,0,0);
        Instantiate(ScoreText, scoreTextSpawnPosition, Quaternion.identity, canvas);
        Destroy(ButtonObject);
    }
}

You can get hold of the instantiated thing directly from instantiate:

scoreTextCopy = Instantiate(ScoreText, scoreTextSpawnPosition, Quaternion.identity, canvas);
scoreTextCopy.GetComponent<Text>().text = "This is the score text";

But if I want to place that thing to already made Canvas, i cant assign Canvas to my prefab, what do i do?

With the reference from the instantiated game object, set the parent to the canvas. :slight_smile:

How does it should look like in code? I’ve been searching google, youtube can’t find anything.

Imagine I have in game directoty “Canvas 1” and I make a clone “Text 1” and I want to assign Text 1 as a child to my canvas. It’s simple right? But when my player dies, I make new one as a clone and I have to assign to a CLONE current canvas. How do i do that?

Well, if the text is something you always want in the scene, I think it would be better to just leave it there & activate/deactivate it when needed. Then you can reference it directly.

Anyways, the specific answer to your question relates to @Baste 's post. You can set the ‘.transform’ of that instantiated game object to the canvas.

But you see, imagine I have canvas with text in my scene and I add Player clone, how do I assign my Canvas, text objects to my Player clone? That’s the main thing I really really need. And how to write a code of that?

Can you do it without a player clone? :slight_smile:

One way is that if you really had a good reason to create a new player, then you could pass a reference to whatever’s needed to your new player.
Say you need a reference to a script that talks to the text on the UI canvas, for updating something.
Well, when you create the player, you could pass a reference to said script.

There are other methods, too. I don’t want to confuse you; I’m happy to offer a few tips to help you get something working, though. =)

If you are instantiating something in the scene, whatever code is in your scene that does the instantiate can have a reference to your canvas and text object. You could then technically assign these to the instantiated object. @methos5k was faster. :slight_smile:

That being said, it may be better to have a script that handles updating your UI rather than having to assign it to your player directly.

How? Imagine I have an object “Main camera” how do I assign that to a Clone?

    public Canvas canvas;
    public Text textScore;
    public Text finalText;
    public Transform Button;


    void Start ()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        Score = 0;
        TimeLeft = 0;
        finalText.text = "";
        canvas = GameObject.FindWithTag("Canvas");
    }

It should be something like this, but it doesnt work.

@Baste pretty much showed how. You instantiate the object. Then you use getComponent to get whatever script you want. His example gets the text component, but you can get your script off the new object(the clone). Then you just assign values to it.

The script that does the instantiate is in your scene and is hooked up to stuff in your scene, so it can assign the values to the newly instantiated objects after creating them.

Just saying it doesn’t work doesn’t tell us anything. What doesn’t work? And you don’t need to find the canvas since you have a public variable. Did you drag and drop all the stuff into the public variables in the inspector?

Well, if that script was on his clone he might be having issues because he didn’t use the other suggestion/approach. Perhaps like if the Text is null, the script exited early :slight_smile:
But who knows. :slight_smile:

Let us know if you’re still stuck :slight_smile: