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 RobbyT15 · Dec 22, 2015 at 09:22 PM · c#unity5

Increasing forward speed when level completes.

I am trying to set up a feature in a space shooter game that works alot like the game Tetris does. With each level the user completes, the hazards fall at a faster speed. However, when making the function call I get the error

 Assets/Scripts/GameController.cs(22,31): warning CS0649: Field `GameController.asteroid' is never assigned to, and will always have its default value `null'

Why would this error be sent back and how do I correctly increase the hazards' falling speed with each passing level?

 public class AsteroidMover:MonoBehaviour{
     private Rigidbody asteroid;
     public float speed;
     
     void Start(){
         asteroid = GetComponent<Rigidbody>();
         asteroid.velocity = transform.forward * speed;
     }
 
     public void increaseSpeed(){
         speed += 0.5f;
         Debug.Log(speed);
     }
 }

public class GameController:MonoBehaviour{ public GameObject hazard; public Vector3 hazard_values; public int min_asteroids; public int max_asteroids; private int hazard_count; public float spawn_wait; public float start_wait; public float wave_wait; private int wave_count = 0; public Text score_text; private int score; public Text restart; public Text game_over_text; private bool game_over; private bool new_game; private AsteroidMover asteroid;

 void Start(){
     game_over = false;
     new_game = false;
     restart.text = "";
     game_over_text.text = "";
     score = 0;
     updateScore();
     StartCoroutine(asteroidWaves());
 }

 void Update(){
     if(new_game){
         if(Input.GetKeyDown(KeyCode.Return)){
             Application.LoadLevel(Application.loadedLevel);
         }
     }
 }
 
 IEnumerator asteroidWaves(){
     yield return new WaitForSeconds(start_wait);
     while(true){
         asteroid = GetComponent<AsteroidMover>();
         wave_count += 1;
         if(asteroid != null){
             if(wave_count > 1){
                 min_asteroids *= wave_count;
                 max_asteroids += min_asteroids;
                 asteroid.increaseSpeed();
             }
         }
         else{
             Debug.Log ("Asteroid is null");
         }

         hazard_count = Random.Range(min_asteroids, max_asteroids);
         for(int i = 0; i < hazard_count; i++){
             Vector3 hazard_position = new Vector3(Random.Range(-hazard_values.x, hazard_values.x), hazard_values.y, hazard_values.z);
             Quaternion hazard_rotation = Quaternion.identity;
             Instantiate(hazard, hazard_position, hazard_rotation);
             yield return new WaitForSeconds(spawn_wait);
         }
         yield return new WaitForSeconds(wave_wait);
     }
 }

}

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 RobbyT15 · Jan 16, 2016 at 04:12 PM

I figured out that I was trying to update the speed in the wrong script. Instead of updating the score in the GameController script, I simply needed to check the wave count inside the asteroidMover script and update it there. Here is what my updated script looks like.

 public class AsteroidMover:MonoBehaviour{
     private Rigidbody asteroid;
     public float speed;
     public GameController game_controller;
     public float new_speed;
     
     void Start(){
         asteroid = GetComponent<Rigidbody>();
         asteroid.velocity = transform.forward * speed;
         GameObject game_object = GameObject.FindWithTag("GameController");
         if(game_object != null){
             game_controller = game_object.GetComponent<GameController>();
         }
         if(game_controller == null){
             Debug.Log("Can't find game controller.");
         }
 
         new_speed = game_controller.wave_count;
 
         if(new_speed > 1){
             increaseSpeed(new_speed);
         }
         else{
             Debug.Log(speed);
         }
     }
 
     public void increaseSpeed(float new_speed){
         speed = speed - (new_speed * 0.5f);
         asteroid.velocity = transform.forward * speed;
         Debug.Log(speed);
     }
 }

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 Zoelovezle · Dec 22, 2015 at 11:33 PM

Since the both scripts are differently attached as components to Gameobjects you need to get the component in your variable before using it.

 // public class GameController : MonoBehavior
 { 
             //Declared a variable
             private AsteroidMover _asteroid  ;
 
            void Start()
 {
                 // You need to get the component before using that variable
                _asteroid = GetComponent<AsteroidMover>() ;
 }


Check this Things :

  1. Check if you have hazard Prefab in your GameController component : line 18

  2. Since your are instantiating hazard as a GameObject in line 67 ,

.

 var hazardClone = (GameObject)Instantiate(hazard, hazard_position, hazard_rotation) ;

Next possible thing can be your hazard Prefab with no Rigidbody component attached.

@RobbyT15

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 RobbyT15 · Dec 22, 2015 at 11:37 PM 0
Share

Ah ok, and once I do that, I should be able to access the function and update the speed with each wave? What do I need to do when I get the error Object Reference not set to an instance of an object?

avatar image Zoelovezle RobbyT15 · Dec 24, 2015 at 02:24 AM 0
Share

Can you tell me at which line it says? In your new Updated Script? & are you making in 3d or 2d?

avatar image RobbyT15 Zoelovezle · Dec 24, 2015 at 11:33 AM 0
Share

I've updated the script in my question. The line it's happening at is 51, the function call to the increaseSpeed function in Asteroid$$anonymous$$over. The asteroid prefab also has a rigidbody attached to it.

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

47 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

Related Questions

Anchor a health bar to a character/enemy 3 Answers

How do I create a Continuous Turn in XR for my VR Rig instead of Snap Turn Provider? 0 Answers

pressing/turning something in VR to do stuff 0 Answers

Can't Resources.Load ScriptableObject in Static class 0 Answers

Getting Parent GameObject from Child? 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