Help FindWithTag Object reference not set to an instance of an object
Hello Problem this Script :
var Model : Transform;
var target : Transform;
function Update () {
if(!Model){
Model = GameObject.FindWithTag ("Player").transform;
target.transform.LookAt(Model);
}
when models object is destroyed, it generates this error: NullReferenceException: Object reference not set to an instance of an object this is because object models was destroyed and the script tries to transform absence. I also tried something like that but without success:
function Update () {
if (models != null){
models = GameObject.FindWithTag ("Player").transform;
target.transform.LookAt(models);
models = models.transform;
}
else
{
return;
}
}
anyone knows the correct solution this problem thanks in advance?
Answer by sailor27 · Mar 07, 2016 at 12:43 PM
the code you have brought and correct and works perfectly Thank you're a big thank you or finally thank you or solved very many thanks
Answer by Fredex8 · Mar 07, 2016 at 10:12 AM
You have the correct approach when using if (models != null){
however if your FindWithTag is inside that condition it is never going to be able to find models.
models = GameObject.FindWithTag ("Player").transform;
if (models != null){
target.transform.LookAt(models);
}
Something like that is what you want but obviously you want to be avoiding using FindWithTag in update unless you actually need to. You should be finding the Player in start and caching the reference to the gameobject and then just getting its transform in update to track the position. I am also not clear why you have this in there after the look at: models = models.transform;
Hello Thanks, but I also tried this script but I keep getting more and error model Player object is instantiated in the scene it is not already in the scene that's why or needs to use FindWithTag
models = GameObject.FindWithTag ("Player").transform;
if (models != null){
target.transform.LookAt(models);
}
error geneted for lack Player tag not for lack target when the $$anonymous$$odel / Player is killed it is error occurs due to lack of GameObject the script can not find more tags Player because it was destroyed I wish the script if you miss the Player tag does not generate error models = models.transform; I wanted to try with this to propose tag Player reinsert
GameObject Player is not already in the scene but is instantiated when you load every single level, that's why or needs to use FindWithTag. when player is killed and then destroyed in that split second error is generated because the script is missing GameObject $$anonymous$$odel tag Player.assenza Player of the tag occurs only if the script and 'tucked under Update function because continuous search item missing tag if the script and inserted in the Function Start and Game Oggetto Tag player is destroyed, not error occurs but var $$anonymous$$odel slot remains empty , But problem checkpoint restart Player
Because the level is not recharge / restart, but Player object is reistanziato last checkpoint Function Start not var $$anonymous$$odel slot
The reason that error is occurring is because you are attempting to get the transform of Player in the same line. If the player doesn't exist it cannot get that. As I said you should find the player then the transform separately. Perhaps I should have written a better example:
models = GameObject.FindWithTag ("Player");
if (models != null){
target.transform.LookAt(models.transform);
}
the code you have brought and correct and works perfectly Thank you're a big thank you or finally thank you or solved very many thanks
Your answer
Follow this Question
Related Questions
Class structure for diferent item types and items that do different things. 0 Answers
Please Help Me: Assets/Scripts/PlayerControaller.cs(34,33): error CS8025: Parsing error 1 Answer
Rigidbody changes reference in play time to the game object that the script is attached to 1 Answer