• 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
Question by smirlianos · Jul 26, 2013 at 09:55 AM · variableinspectorchangeintreal-time

Variable not changing on real-time problem

Hello,

I have a very weird problem. I make a game where you control a spaceship and destroy comets. If a comet hits you, you lose one life.

The problem is that I have set a variable that holds the lives

 var curLives : int;

but if a comet hits me it doesn't change. However, when I click the play button again to stop testing the game it goes to the number it was supposed to be as i was playing the game. In other words, if I have 5 lives at the beginning of the game and 3 comets hit me, when I stop playing the game, the variable on the inspector goes to 2.

This is really confusing.

I also have this line on the start function, so it sets the lives as another variable.

 function Start() {
     curLives = maxLives;
 }

But it doesn't change....

here are my scripts. If anyone can help me :)

Js_Spaceship :

 #pragma strict
 var speed : int;
 var missle : GameObject;
 var spawner : GameObject;
 private var canShoot : boolean = true;
 
 var curLives : int;
 var maxLives : int;
 var livesObj1 : GameObject;
 var livesObj2 : GameObject;
 var livesObj3 : GameObject;
 var livesObj4 : GameObject;
 var livesObj5 : GameObject;
 
 
 function Start () {
     var curLives = maxLives;
 }
 
 function Update () {
     transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * speed,0,0);
     if(transform.position.x >= 4)
     {
         transform.position.x = 4;
     }
     if(transform.position.x <= -4)
     {
         transform.position.x = -4;
     }
     Shoot();
     
     //life calculations
     Lifer ();
 }
 
 function Shoot () {
 
     if(Input.GetKey(KeyCode.Space) && canShoot)
     {
         Instantiate(missle, spawner.transform.position, spawner.transform.rotation);
         canShoot = false;
         yield WaitForSeconds(0.5);
         canShoot = true;
     }
 }
 
 function Lifer () {
     if(curLives == 5)
     {
         livesObj1.active = true;
         livesObj2.active = true;
         livesObj3.active = true;
         livesObj4.active = true;
         livesObj5.active = true;
     }
     if(curLives == 4)
     {
         livesObj1.active = true;
         livesObj2.active = true;
         livesObj3.active = true;
         livesObj4.active = true;
         livesObj5.active = false;
     }
     if(curLives == 3)
     {
         livesObj1.active = true;
         livesObj2.active = true;
         livesObj3.active = true;
         livesObj4.active = false;
         livesObj5.active = false;
     }
     if(curLives == 2)
     {
         livesObj1.active = true;
         livesObj2.active = true;
         livesObj3.active = false;
         livesObj4.active = false;
         livesObj5.active = false;
     }
     if(curLives == 1)
     {
         livesObj1.active = true;
         livesObj2.active = false;
         livesObj3.active = false;
         livesObj4.active = false;
         livesObj5.active = false;
     }
 }



Js_Comet :

 #pragma strict
 var speed : int;
 var player : Js_Spaceship;
 function Start () {
 
 }
 
 function Update () {
     transform.Translate(0,-speed * Time.deltaTime,0,Space.World);
 }
 
 function OnCollisionEnter (other : Collision) {
     if(other.gameObject.tag == "Player")
     {
         player.curLives --;
     }
 }
Comment

People who like this

0 Show 2
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 GC1983 · Jul 26, 2013 at 10:06 AM 0
Share

Well, lets see...

Did you make sure the player variable was assigned to your Spaceship object? If so, you can try using OnTriggerEnter() instead of OnCollisionEnter(). Its strange, but its worked for me before. Also, did you make sure the player object is tagged "Player"? Sorry if these come off as dumb questions, but we have all wracked our brains with the smallest details. You could try using Debug.Log to be sure statements are being accessed.

avatar image RPGstandard · Jul 26, 2013 at 10:08 AM 0
Share

Im dont really use JS, but it seams like your missing a call to the Lifer function inside the the collision function that would update the in game life total.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Jul 26, 2013 at 10:55 AM

Your problem is that your "player" variable in your comet script doesn't point to the player. It points to the players prefab and is changing the value in the prefab asset. You have to link the real player at runtime when you instantiated the comets.

The better implementation is something like that:

 function OnCollisionEnter (other : Collision)
 {
     if(other.gameObject.tag == "Player")
     {
         // Get the Js_Spaceship instance from the object we collided with.
         var player = other.gameObject.Getcomponent(Js_Spaceship);
         player.curLives --;
     }
 }
Comment

People who like this

0 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 GC1983 · Jul 26, 2013 at 05:53 PM 0
Share

Keep in mind you only need to call the GetComponent once. You can put that line of code in your Awake function. It can get expensive calling for that everytime it collides.

avatar image smirlianos · Jul 27, 2013 at 09:54 AM 0
Share

If I put it on Awake() it doesn't work because of the "other" that's only inside the OnCollisionEnter function. Why is better on the awake? One time every comet collides

avatar image GC1983 · Jul 27, 2013 at 04:45 PM 0
Share

Its just the way bunny did it in your situation. When you use GameObject.Find() or GetComponent , etc.... You can put it in Awake() or Start(). These will only need to be called once at game start. It saves memory.

avatar image

Answer by GC1983 · Jul 26, 2013 at 10:19 AM

Oh wait, I think I know the issue...Youre using rigidbody transform.Translate(). That is a physics based movement and ignores collision. I would recommend using a Character Controller for your player ship so can very accurately move your player around and any collision made with outside colliders (your enemies), it will register and respond.

I had this same issue and learned that CCs are very useful.

 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                 charController.Move(moveDirection * Time.deltaTime);

That is your base XY movement. Attach a CC to your object and test the collision.

Comment
VoKhab

People who like this

1 Show 2 · 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 smirlianos · Jul 26, 2013 at 10:24 AM 0
Share

What should I add? Character motor?

avatar image GC1983 · Jul 26, 2013 at 10:34 AM 0
Share

you can use the premade controllers if you want. Theyre a lot to read over if youre not sure of what youre doing. I would recommend reading up on GetAxis and check out YouTube and other topics related to that topic that is something you can work off of for your particular controller situation. Its the best way to learn how it all works.

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

18 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

Related Questions

Public variable not showing in Inspector(Solved) 1 Answer

Variable data type for two ints (x and y) in inspector 3 Answers

Unity crashes when I change values on inspector 0 Answers

GetComponent from string name? 3 Answers

'Cannot convert int to String' 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