Why does FindObjectOfType().transform return a null reference
morning all; I have the following in my script
using UnityEngine;
public class Spell : ScriptableObject
{
Transform tempObject;
private void Awake()
{
castSpell = effectPrefab.GetComponent<CastSpell>();
tempObject = FindObjectOfType<TempObject>().transform;
}
}
Which keeps returning a null reference exception for tempObject.
Here is a screenshot of my Hierarchy and Inspector windows
As part of my process, I altered the code to the following:
private void Awake()
{
castSpell = effectPrefab.GetComponent<CastSpell>();
TempObject tempObj = FindObjectOfType<TempObject>();
tempObject = tempObj.transform;
}
and this has allowed me to determine that the null reference is referring to the transform rather than the object itself and I'm not sure how to resolve this error.
try to find the same in start method bro, may it works.
Answer by tormentoarmagedoom · Sep 30, 2019 at 07:31 AM
Hello.
The error is beacause there is no object like that in the scene.
You wilkl never get a nul lreference error using FindObjectOfType()
ONLY will get the error if trying to acces one of its components (like transform) or chango one of its properties but the object is nulll.
So, the problem is there is no object like that in the scene.
Bye!
Your answer
![](https://koobas.hobune.stream/wayback/20220612212756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NullException Error while instantiating gameobjects 0 Answers
NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer
Why does GameObject.Find() work in ones and not in others? 1 Answer
NullRefrenceException: Object refrence not set to an instance of an object. 0 Answers