Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by buxton4life · Aug 24, 2012 at 05:59 PM · javascriptiosxcodegameobject.findsmooth follow

Smooth Follow 2D works but Null Reference Error Remains

Good morrow people of Unity,

I have a smooth follow script attached to the main camera which finds and follows the instantiated ball. This works fine.

Though between the point of the ball being destroyed and re-spawned the null reference appears. This is because the code is returning NULL to the GameObject.FInd bit. It does not effect the running of the game in unity though when I try to publish in Xcode this error stops it working :(

My question is how do I get around this. I was thinking maybe attach the script to the ball so that it only runs when in the scene??? But i'm not sure.

Also If there is a method that doesn't involve gameObject.Find that would be awesome as I am publishing for IOS.

Any guidance would be extraordinary! Thanks legends.

CODE:

       var smoothTime = 0.3;
       private var velocity : Vector2;
       var thisTransform : Transform;
       var target : Transform;
       
       function Update()
        {
     thisTransform = transform;
         
     target = GameObject.Find("ball(Clone)").transform;
     
     if(!target){
 
     } else if (target) {
 
     thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, 
         target.position.x, velocity.x, smoothTime);
     
         thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, 
         target.position.y, velocity.y, smoothTime);
         } 
       }
Comment
Add comment · Show 6
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 Mander · Aug 24, 2012 at 06:10 PM 1
Share

try to use tags and see what happens, this is the syntx: gameObject.FindWithTag("player").transform;

or maybe just don't destroy the ball and change its position. idk just another option.

avatar image OperationDogBird · Aug 24, 2012 at 06:17 PM 1
Share

GameObject.Find... is fine to use, just not the way you are using it (running the find every frame). Place it in start

 public var ball:GameObject;
 
 function Start()
 {
     ball=GameObject.Find("ball(Clone)");
 }

or just set the variable from the ball itself. What line is throwing the null??

avatar image Mander · Aug 24, 2012 at 06:20 PM 0
Share

it is working for him at start, but when he dies or rather destroys the ball and spawns another ball, it isnt working. am i right? @buxton4life

avatar image OperationDogBird · Aug 24, 2012 at 06:29 PM 1
Share

Something like this would be better than constantly running the Find, tho setting target from the ball itself would be best.

 if(!target){
     target = GameObject.Find("ball(Clone)").transform;
 } else {

 thisTransform.position.x = $$anonymous$$athf.SmoothDamp( thisTransform.position.x, 
    target.position.x, velocity.x, smoothTime);

     thisTransform.position.y = $$anonymous$$athf.SmoothDamp( thisTransform.position.y, 
    target.position.y, velocity.y, smoothTime);
    } 
   }
avatar image buxton4life · Aug 24, 2012 at 06:51 PM 0
Share

Thanks everyone for the advice.

Yeah @$$anonymous$$ander the follow script runs fine but I just want to get rid of the errors in the log. As the game runs fine in Unity and remote but not Xcode when built.

Absolutely running a find in update was pretty stupid :/

I will try setting the variable from the ball itself, and use that code snippet above it looks much better.

I will report back with the results. Gracias Amigos.

Show more comments

1 Reply

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

Answer by Caliber Mengsk · Aug 24, 2012 at 07:25 PM

NullReferenceException errors mean that the object you are trying to get does not exist. In this case, the gameobject.find is not finding anything, meaning the variable ball is not set, throwing the null error.

What I would do is set the ball variable through the respawn script, like this: respawnScript:

 function OnRespawn()
 {
     //NOTE: this is just an example function name, use your function, obviously
     Camera.main.GetComponent(FollowObjectScript).target = transform;
 }


FollowObjectScript: var smoothTime = 0.3;

 private var velocity : Vector2;
 private var thisTransform : Transform;
 
 public var target : Transform;
 
 
 function Update()
 {
     if(target) {
         thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, 
         target.position.x, velocity.x, smoothTime);
 
         thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, 
         target.position.y, velocity.y, smoothTime);
     } 
     thisTransform = transform;
 }

This is also much more efficient then running the find command every loop. The find command loops through all game objects in the scene. That means the more objects you have in the scene, the slower it will go, and is a general waste of speed. Sure, it's nice to use some times, but really should not be run every frame. The way I did it above is much faster as it's just setting the reference value, and only does it on the respawn event.

Hope this helps.

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

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

10 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

Related Questions

iOS Javascript problem 0 Answers

Typed component declaration returning null in iOS 1 Answer

BuildPlayer and Plugins on Ios 1 Answer

Where are the sound files inside XCode? 1 Answer

iOS build - Pointing to the right Xcode 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