• 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 Vlad_96 · Mar 24, 2018 at 11:01 AM · 2d gameoptimizationscript errorlaggysmoothing

Q: How can I resolve my error and how to optimize this script so the game runs smooth ?

I want to resolve the error I have attached but I don't know how.

Here is the script where the error takes place (This is the line in question:

Vector2 direction = (Vector2)target.position - rb.position;)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class HomingMissile : MonoBehaviour {
 
 
 
     
 
 
     public Transform target;
 
     public float delay ;
 
     public float speed = 5f;
     public float rotateSpeed = 200f;
     public GameObject ExplosionEffect;
 
     private Rigidbody2D rb;
 
 
 
 
     // Use this for initialization
     IEnumerator Start () {
 
 
         yield return new WaitForSeconds(delay);
         rb = GetComponent<Rigidbody2D>();
 
     }
 
 
 
 
 
     void FixedUpdate () {
 
         
         Vector2 direction = (Vector2)target.position - rb.position;
 
         direction.Normalize();
 
         float rotateAmount = Vector3.Cross(direction, transform.up).z;
 
         rb.angularVelocity = -rotateAmount * rotateSpeed;
 
         rb.velocity = transform.up * speed;
 
                          }
 
 
 
 
                        
     void OnTriggerEnter2D ()
     {
         
         Destroy(gameObject);
          Instantiate(ExplosionEffect, transform.position, transform.rotation);
         
         ScoreScript.scoreValue += 1;
 
 
     }
 
 
 
 
 
 
 }
 


[1]: /storage/temp/113594-scr1unt.png

screen-shot-2018-03-24-at-125253-pm.png (349.9 kB)
scr1unt.png (198.1 kB)
Comment
Add comment · Show 1
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 mani3307 · Mar 24, 2018 at 11:27 AM 0
Share

Optimization is a vast part refer unity learn for tutorials

2 Replies

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

Answer by fafase · Mar 24, 2018 at 12:27 PM

Your error is due to the delay you set before getting the Rigidbody.

Your Start method will wait for delay amount of seconds. In the meanwhile, your FixedUpdate is running but the rigidbody reference is null.

I don't know why you are stalling in the Start but either you don't:

 void Start() 
 {
      rb = GetComponent<Rigidbody>();
 }

or if you have a reason to do so, skip the FixedUpdate until the rigidbody is valid:

 void FixedUpdate()
 {
       if(rb == null) { return; }
 }


Comment
Add comment · Show 1 · 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 Vlad_96 · Mar 24, 2018 at 12:40 PM 0
Share

Thank you very much ! It solved my issue.

avatar image
0

Answer by mani3307 · Mar 24, 2018 at 11:26 AM

@Vlad_96 I am also a beginner, so I will explain what I know 1.) don't use Start as a coroutine it will only runs once. use void Start() { rb.GetComponent(); } move dealy to a separate coroutine 2.)I think You are getting Null reference exceptions at line 45 because you are using "[RequireComponent(typeof(Rigidbody2D))]" incorrectly. and it is not rigidbody2d to the missile make a prefab of the missile and attach rigidbody2d to it and use it instead 3.)use this code in update Vector2 direction = (Vector2)(target.position - rb.position); direction.Normalize(); as they require to update constantly

Comment
Add comment · Show 1 · 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 Vlad_96 · Mar 24, 2018 at 12:40 PM 0
Share

Thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

90 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 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

Laggy behaviour after two object with colliders are generated on the same place 0 Answers

Unity WebGL 60FPS on Windows, 30FPS on MacOS 0 Answers

Admob plugin lagging when downloading ad 1 Answer

Loading Cards with different langauges 0 Answers

When should I use object pool? 2 Answers


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