How to attach on screen joystick prefab to Gameobject?

Hello guys! I really need your help,

How to attach on screen joystick prefab to GameObject?

I’m developing a multiplayer game for Android and I’ve searched so many forums, videos and etc. about attaching on screen joystick into my player prefab and I cant find anything, if i did, it may too hard for me to understand the steps since im just a beginner.

Because I cant drag n drop the joystick to my prefab player. And the player is depending on the joystick to move or rotate. And i can’t just put the player prefab to the hierarchy/scene to drag n drop the joystick to player prefab, because since its a multiplayer the players will instantiated by spawner.


I hope you guys help me, anything is accepted.
Thank you!

Late and long response… but bear with me… these are the steps I took

  1. Created a prefab of my Canvas (which had my joystick in it)
  2. Made sure both prefabs were in the same folder (the resources folder in my case)
  3. I then dragged the Canvas prefab (which had the joystick) under the prefab of my character (in the hierarchy)
    -that way it instantiates with the character (no extra code needed)
  4. then I right clicked the Canvas prefab and went to prefab, and I completely unpacked the prefab
  5. Then I was able to drag the joystick into the character!!! Hooray!!!

Note: this ended up working even better for me as the Canvas instantiating with my character helped reduce certain glitches
Let me know if it works!

Hey, I had the same problem and I fixed it with the help of @jsaada2003 's solution. Big shout out to him!

To simplify his answer, here is what did:

  1. Create a Canvas and put the joystick into the Canvas
  2. Put the Canvas into the character
  3. In the character’s inspector, drag the joystick (which is in the Canvas which is in the character itself) into the character’s script’s joystick prefab variable.
  4. Then drag the character into your assets to make it a prefab. The prefab will contain the joystick. This way when initiating the character, a new Canvas with a joystick will be created and your character’s script will keep a reference of the joystick automatically.

The @YilinYang 's method works too, but It would be nice if the Canvas is independent of any Object and should be treated as a parent object. In order to add a script to a prefab after starting the game, the best way is to do it at the time of Instantiation,
Do Instantiate and attach it to a variable, then find your script which you want to attach and put that in the variable created by Instantiate.

Maybe this code gives you an idea. In this way the Canvas is preserved as an independent object and our work is done.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class PlaceManager : MonoBehaviour
{
    private PlaceIndicator placeIndicator;
    private GameObject NewPlacedObject;
    private int ObjectCount = 0;
    public GameObject ObjectToPlace;
    public FixedJoystick joystickScript;
    
    void Start()
    {
        joystickScript = FindObjectOfType<FixedJoystick>();
        placeIndicator = FindObjectOfType<PlaceIndicator>();
    }

    public void PlaceObject()
    {
        if(ObjectCount == 0)
        {
            NewPlacedObject = Instantiate(ObjectToPlace, placeIndicator.transform.position, placeIndicator.transform.rotation);
            Transform playerJoeTransform = NewPlacedObject.transform.Find("playerJoe");
            playerJoeTransform.GetComponent<PlayerController>()._joystick = joystickScript;
            ObjectCount++;
        }
        else
        {
            Debug.Log("Object already placed");
        }
    }
}