Which is proper way to use a prefab in scripting?

I normally use like the following

public GameObject placedPrefab;

Instantiate(PlacePrefab, hitPose.position, hitPose.rotation);

or
Why do we do like the following way using get and set?

public GameObject PlacePrefab
    {
        get
        {
            return placedPrefab;
        }

        set
        {
            placedPrefab = value;
        }

    }

if(arRaycastManager.Raycast(touchPosition,hits,UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
        {
            var hitPose = hits[0].pose;

            Instantiate(PlacePrefab, hitPose.position, hitPose.rotation);
        }

Which is the correct way.First method is easy.The second method we are making the GameObject private to hide the GameObject.Is that the use of the above code?

If you use the second method, you still have to define placedPrefab which is best done by using a public variable.

1 Like

Keep it simple, treat the prefab just as a regular gameobject, instantiate it and so forth.

For successful game development in Unity, my advice is to lean toward whatever is the most direct, brute force method. This is because Unity already is a ton of (arguably) over-engineered structure as it is.