Cannot Instantiate objects

I had a heck of a time getting this to work - despite seemingly following the instructions twice, no code errors - but also no damage numbers. Eventually I just went to the Course Resources and copied the code for damagetext.cs - damagetextspawner.cs and health.cs … and now I get numbers and errors:

Cannot instantiate objects with a parent which is persistent. New object will be created without a parent.
UnityEngine.Object:Instantiate<RPG.UI.DamageText.DamageText> (RPG.UI.DamageText.DamageText,UnityEngine.Transform)
RPG.UI.DamageText.DamageTextSpawner:Spawn (single) (at Assets/Characters/Scripts/DamageTextSpawner.cs:18)
UnityEngine.Events.UnityEvent:Invoke ()
RPG.Attributes.Health:TakeDamage (UnityEngine.GameObject,single) (at Assets/Characters/Scripts/Attributes/Health.cs:64)
RPG.Combat.Fighter:Hit () (at Assets/Characters/Scripts/Fighter.cs:105)

1 Like

This is usually because you are trying to set the Parent in the Instantiate method to a prefab instead of a scene object.

Paste in your DamageTextSpawner.cs script and we’ll have a look.

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

namespace RPG.UI.DamageText
{
    public class DamageTextSpawner : MonoBehaviour
    {
        [SerializeField] DamageText damageTextPrefab = null;

        void Start()
        {
            Spawn(11);
        }

        public void Spawn(float damageAmount)
        {
            DamageText instance = Instantiate<DamageText>(damageTextPrefab, transform);
        }
    }
}

Ok, the code looks right.
One more thing to check: Make sure that in your Health component, that you are Invoking on the DamageTextSpawner that is on the Player, not selecting the a prefab in the assets folder.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms