Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sailor27 · Mar 07, 2016 at 07:23 AM · scripting problem

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sailor27 · Mar 07, 2016 at 10:30 AM 0
Share

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

avatar image Fredex8 sailor27 · Mar 07, 2016 at 12:12 PM 0
Share

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);
   }
avatar image sailor27 Fredex8 · Mar 07, 2016 at 12:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

cannot convert type HealthandInventory to int 0 Answers

Bullet doesn`t die when enemy destory 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges