Why the score didn't work properly in unity C#
I made a scoring system based on destroyed balls, so on start, the score is 82, and each ball destroyed should decrease the score by 1.
i made a script for the ball, but the score didn't work properly, can you tell me what's wrong in my code?
This is the ball script:
public class Ball : MonoBehaviour {
private int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
Thanks,
c# unity3d
add a comment |
I made a scoring system based on destroyed balls, so on start, the score is 82, and each ball destroyed should decrease the score by 1.
i made a script for the ball, but the score didn't work properly, can you tell me what's wrong in my code?
This is the ball script:
public class Ball : MonoBehaviour {
private int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
Thanks,
c# unity3d
What do you mean by "it didn't work properly?"
– Robert Harvey♦
Nov 17 '18 at 18:37
@RobertHarvey I mean the score decrease to 81 and it didn't change even other balls are destroyed
– Taik
Nov 17 '18 at 18:41
I think the problem might be that each ball has its own score.
– Leo Bartkus
Nov 17 '18 at 18:55
Put some logs or use the debugger.
– Iggy
Nov 17 '18 at 19:19
Every ball has a private int for the count that is set to 82, its not a static int or anything so everytime a ball updates the text it is updating it with its own verision of count, which decremented even if on the next frame its personal private count is 81.
– Eddge
Nov 17 '18 at 19:22
add a comment |
I made a scoring system based on destroyed balls, so on start, the score is 82, and each ball destroyed should decrease the score by 1.
i made a script for the ball, but the score didn't work properly, can you tell me what's wrong in my code?
This is the ball script:
public class Ball : MonoBehaviour {
private int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
Thanks,
c# unity3d
I made a scoring system based on destroyed balls, so on start, the score is 82, and each ball destroyed should decrease the score by 1.
i made a script for the ball, but the score didn't work properly, can you tell me what's wrong in my code?
This is the ball script:
public class Ball : MonoBehaviour {
private int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
Thanks,
c# unity3d
c# unity3d
asked Nov 17 '18 at 18:35
TaikTaik
657
657
What do you mean by "it didn't work properly?"
– Robert Harvey♦
Nov 17 '18 at 18:37
@RobertHarvey I mean the score decrease to 81 and it didn't change even other balls are destroyed
– Taik
Nov 17 '18 at 18:41
I think the problem might be that each ball has its own score.
– Leo Bartkus
Nov 17 '18 at 18:55
Put some logs or use the debugger.
– Iggy
Nov 17 '18 at 19:19
Every ball has a private int for the count that is set to 82, its not a static int or anything so everytime a ball updates the text it is updating it with its own verision of count, which decremented even if on the next frame its personal private count is 81.
– Eddge
Nov 17 '18 at 19:22
add a comment |
What do you mean by "it didn't work properly?"
– Robert Harvey♦
Nov 17 '18 at 18:37
@RobertHarvey I mean the score decrease to 81 and it didn't change even other balls are destroyed
– Taik
Nov 17 '18 at 18:41
I think the problem might be that each ball has its own score.
– Leo Bartkus
Nov 17 '18 at 18:55
Put some logs or use the debugger.
– Iggy
Nov 17 '18 at 19:19
Every ball has a private int for the count that is set to 82, its not a static int or anything so everytime a ball updates the text it is updating it with its own verision of count, which decremented even if on the next frame its personal private count is 81.
– Eddge
Nov 17 '18 at 19:22
What do you mean by "it didn't work properly?"
– Robert Harvey♦
Nov 17 '18 at 18:37
What do you mean by "it didn't work properly?"
– Robert Harvey♦
Nov 17 '18 at 18:37
@RobertHarvey I mean the score decrease to 81 and it didn't change even other balls are destroyed
– Taik
Nov 17 '18 at 18:41
@RobertHarvey I mean the score decrease to 81 and it didn't change even other balls are destroyed
– Taik
Nov 17 '18 at 18:41
I think the problem might be that each ball has its own score.
– Leo Bartkus
Nov 17 '18 at 18:55
I think the problem might be that each ball has its own score.
– Leo Bartkus
Nov 17 '18 at 18:55
Put some logs or use the debugger.
– Iggy
Nov 17 '18 at 19:19
Put some logs or use the debugger.
– Iggy
Nov 17 '18 at 19:19
Every ball has a private int for the count that is set to 82, its not a static int or anything so everytime a ball updates the text it is updating it with its own verision of count, which decremented even if on the next frame its personal private count is 81.
– Eddge
Nov 17 '18 at 19:22
Every ball has a private int for the count that is set to 82, its not a static int or anything so everytime a ball updates the text it is updating it with its own verision of count, which decremented even if on the next frame its personal private count is 81.
– Eddge
Nov 17 '18 at 19:22
add a comment |
2 Answers
2
active
oldest
votes
When you call Destroy(gameObject)
this GameObject is destroyed with this Ball
script that is attached to it.
Separate your score system from the ball collision detection system.
1.Create an Empty GameObject and name it "ScoreSystem".
2.Create a script and name it "ScoreSys" then use the code below inside it.
3.Attach it to the "ScoreSystem". GameObject.
public class ScoreSys : MonoBehaviour
{
public Text TextCount;
private int _Count;
public int Count
{
get
{
return _Count;
}
set
{
if (_Count != value)
{
_Count = value;
TextCount.text = _Count.ToString();
}
}
}
void Start()
{
Count = 82;
TextCount.text = _Count.ToString();
}
}
The score system now has it's own code and GameObject and it won't be destroyed. Also, Text
is also updated when score changes.
Now, you also have to separate the Ball
script. Simply find the "ScoreSystem" GameObject, get the ScoreSys
component attached to it and updated the score. The Ball
script should be attached to the Ball Object that will be destroyed.
Note that I have no idea when the score should update but below is the translation of your current code. You may need to make some changes to get it working properly.
public class Ball : MonoBehaviour
{
ScoreSys scoreSys;
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
GameObject scoreObj = GameObject.Find("ScoreSystem");
scoreSys = scoreObj.GetComponent<ScoreSys>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (rb.position.y < -3)
{
scoreSys.Count--;
Destroy(gameObject);
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
scoreSys.Count--;
Destroy(gameObject);
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
}
Trying to decide if I should post an answer showing how to use a delegate to do this as well lol.
– Eddge
Nov 17 '18 at 19:27
1
@Eddge Delegate is another way to do this but it's not related to the problem which is just the object being destroyed and removing the current score.
– Programmer
Nov 17 '18 at 19:33
Yea, I know I was just thinking it would cool to show another way to do it without having to connect objects in the editor and using GameObject.find or findObjectsOfType =)
– Eddge
Nov 17 '18 at 19:36
1
That makes sense. You don't need my approval to do so.
– Programmer
Nov 17 '18 at 19:40
1
Amazing answer! Thank you so much!
– Taik
Nov 17 '18 at 19:41
add a comment |
If you make Count a static, then each Ball object will share the same value in memory. As someone else pointed-out, if there are more than one of these Ball objects, they would each have their own idea of the value for Count.
static int Count;
Make it static in the class (add one word to your source code) and I think it will act as you desire.
Also, you don't need to explicitly declare 'private' it is the default.
public class Ball : MonoBehaviour {
static int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354288%2fwhy-the-score-didnt-work-properly-in-unity-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you call Destroy(gameObject)
this GameObject is destroyed with this Ball
script that is attached to it.
Separate your score system from the ball collision detection system.
1.Create an Empty GameObject and name it "ScoreSystem".
2.Create a script and name it "ScoreSys" then use the code below inside it.
3.Attach it to the "ScoreSystem". GameObject.
public class ScoreSys : MonoBehaviour
{
public Text TextCount;
private int _Count;
public int Count
{
get
{
return _Count;
}
set
{
if (_Count != value)
{
_Count = value;
TextCount.text = _Count.ToString();
}
}
}
void Start()
{
Count = 82;
TextCount.text = _Count.ToString();
}
}
The score system now has it's own code and GameObject and it won't be destroyed. Also, Text
is also updated when score changes.
Now, you also have to separate the Ball
script. Simply find the "ScoreSystem" GameObject, get the ScoreSys
component attached to it and updated the score. The Ball
script should be attached to the Ball Object that will be destroyed.
Note that I have no idea when the score should update but below is the translation of your current code. You may need to make some changes to get it working properly.
public class Ball : MonoBehaviour
{
ScoreSys scoreSys;
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
GameObject scoreObj = GameObject.Find("ScoreSystem");
scoreSys = scoreObj.GetComponent<ScoreSys>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (rb.position.y < -3)
{
scoreSys.Count--;
Destroy(gameObject);
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
scoreSys.Count--;
Destroy(gameObject);
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
}
Trying to decide if I should post an answer showing how to use a delegate to do this as well lol.
– Eddge
Nov 17 '18 at 19:27
1
@Eddge Delegate is another way to do this but it's not related to the problem which is just the object being destroyed and removing the current score.
– Programmer
Nov 17 '18 at 19:33
Yea, I know I was just thinking it would cool to show another way to do it without having to connect objects in the editor and using GameObject.find or findObjectsOfType =)
– Eddge
Nov 17 '18 at 19:36
1
That makes sense. You don't need my approval to do so.
– Programmer
Nov 17 '18 at 19:40
1
Amazing answer! Thank you so much!
– Taik
Nov 17 '18 at 19:41
add a comment |
When you call Destroy(gameObject)
this GameObject is destroyed with this Ball
script that is attached to it.
Separate your score system from the ball collision detection system.
1.Create an Empty GameObject and name it "ScoreSystem".
2.Create a script and name it "ScoreSys" then use the code below inside it.
3.Attach it to the "ScoreSystem". GameObject.
public class ScoreSys : MonoBehaviour
{
public Text TextCount;
private int _Count;
public int Count
{
get
{
return _Count;
}
set
{
if (_Count != value)
{
_Count = value;
TextCount.text = _Count.ToString();
}
}
}
void Start()
{
Count = 82;
TextCount.text = _Count.ToString();
}
}
The score system now has it's own code and GameObject and it won't be destroyed. Also, Text
is also updated when score changes.
Now, you also have to separate the Ball
script. Simply find the "ScoreSystem" GameObject, get the ScoreSys
component attached to it and updated the score. The Ball
script should be attached to the Ball Object that will be destroyed.
Note that I have no idea when the score should update but below is the translation of your current code. You may need to make some changes to get it working properly.
public class Ball : MonoBehaviour
{
ScoreSys scoreSys;
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
GameObject scoreObj = GameObject.Find("ScoreSystem");
scoreSys = scoreObj.GetComponent<ScoreSys>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (rb.position.y < -3)
{
scoreSys.Count--;
Destroy(gameObject);
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
scoreSys.Count--;
Destroy(gameObject);
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
}
Trying to decide if I should post an answer showing how to use a delegate to do this as well lol.
– Eddge
Nov 17 '18 at 19:27
1
@Eddge Delegate is another way to do this but it's not related to the problem which is just the object being destroyed and removing the current score.
– Programmer
Nov 17 '18 at 19:33
Yea, I know I was just thinking it would cool to show another way to do it without having to connect objects in the editor and using GameObject.find or findObjectsOfType =)
– Eddge
Nov 17 '18 at 19:36
1
That makes sense. You don't need my approval to do so.
– Programmer
Nov 17 '18 at 19:40
1
Amazing answer! Thank you so much!
– Taik
Nov 17 '18 at 19:41
add a comment |
When you call Destroy(gameObject)
this GameObject is destroyed with this Ball
script that is attached to it.
Separate your score system from the ball collision detection system.
1.Create an Empty GameObject and name it "ScoreSystem".
2.Create a script and name it "ScoreSys" then use the code below inside it.
3.Attach it to the "ScoreSystem". GameObject.
public class ScoreSys : MonoBehaviour
{
public Text TextCount;
private int _Count;
public int Count
{
get
{
return _Count;
}
set
{
if (_Count != value)
{
_Count = value;
TextCount.text = _Count.ToString();
}
}
}
void Start()
{
Count = 82;
TextCount.text = _Count.ToString();
}
}
The score system now has it's own code and GameObject and it won't be destroyed. Also, Text
is also updated when score changes.
Now, you also have to separate the Ball
script. Simply find the "ScoreSystem" GameObject, get the ScoreSys
component attached to it and updated the score. The Ball
script should be attached to the Ball Object that will be destroyed.
Note that I have no idea when the score should update but below is the translation of your current code. You may need to make some changes to get it working properly.
public class Ball : MonoBehaviour
{
ScoreSys scoreSys;
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
GameObject scoreObj = GameObject.Find("ScoreSystem");
scoreSys = scoreObj.GetComponent<ScoreSys>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (rb.position.y < -3)
{
scoreSys.Count--;
Destroy(gameObject);
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
scoreSys.Count--;
Destroy(gameObject);
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
}
When you call Destroy(gameObject)
this GameObject is destroyed with this Ball
script that is attached to it.
Separate your score system from the ball collision detection system.
1.Create an Empty GameObject and name it "ScoreSystem".
2.Create a script and name it "ScoreSys" then use the code below inside it.
3.Attach it to the "ScoreSystem". GameObject.
public class ScoreSys : MonoBehaviour
{
public Text TextCount;
private int _Count;
public int Count
{
get
{
return _Count;
}
set
{
if (_Count != value)
{
_Count = value;
TextCount.text = _Count.ToString();
}
}
}
void Start()
{
Count = 82;
TextCount.text = _Count.ToString();
}
}
The score system now has it's own code and GameObject and it won't be destroyed. Also, Text
is also updated when score changes.
Now, you also have to separate the Ball
script. Simply find the "ScoreSystem" GameObject, get the ScoreSys
component attached to it and updated the score. The Ball
script should be attached to the Ball Object that will be destroyed.
Note that I have no idea when the score should update but below is the translation of your current code. You may need to make some changes to get it working properly.
public class Ball : MonoBehaviour
{
ScoreSys scoreSys;
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
GameObject scoreObj = GameObject.Find("ScoreSystem");
scoreSys = scoreObj.GetComponent<ScoreSys>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (rb.position.y < -3)
{
scoreSys.Count--;
Destroy(gameObject);
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
scoreSys.Count--;
Destroy(gameObject);
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
}
edited Nov 17 '18 at 19:29
answered Nov 17 '18 at 19:24


ProgrammerProgrammer
76.5k1087154
76.5k1087154
Trying to decide if I should post an answer showing how to use a delegate to do this as well lol.
– Eddge
Nov 17 '18 at 19:27
1
@Eddge Delegate is another way to do this but it's not related to the problem which is just the object being destroyed and removing the current score.
– Programmer
Nov 17 '18 at 19:33
Yea, I know I was just thinking it would cool to show another way to do it without having to connect objects in the editor and using GameObject.find or findObjectsOfType =)
– Eddge
Nov 17 '18 at 19:36
1
That makes sense. You don't need my approval to do so.
– Programmer
Nov 17 '18 at 19:40
1
Amazing answer! Thank you so much!
– Taik
Nov 17 '18 at 19:41
add a comment |
Trying to decide if I should post an answer showing how to use a delegate to do this as well lol.
– Eddge
Nov 17 '18 at 19:27
1
@Eddge Delegate is another way to do this but it's not related to the problem which is just the object being destroyed and removing the current score.
– Programmer
Nov 17 '18 at 19:33
Yea, I know I was just thinking it would cool to show another way to do it without having to connect objects in the editor and using GameObject.find or findObjectsOfType =)
– Eddge
Nov 17 '18 at 19:36
1
That makes sense. You don't need my approval to do so.
– Programmer
Nov 17 '18 at 19:40
1
Amazing answer! Thank you so much!
– Taik
Nov 17 '18 at 19:41
Trying to decide if I should post an answer showing how to use a delegate to do this as well lol.
– Eddge
Nov 17 '18 at 19:27
Trying to decide if I should post an answer showing how to use a delegate to do this as well lol.
– Eddge
Nov 17 '18 at 19:27
1
1
@Eddge Delegate is another way to do this but it's not related to the problem which is just the object being destroyed and removing the current score.
– Programmer
Nov 17 '18 at 19:33
@Eddge Delegate is another way to do this but it's not related to the problem which is just the object being destroyed and removing the current score.
– Programmer
Nov 17 '18 at 19:33
Yea, I know I was just thinking it would cool to show another way to do it without having to connect objects in the editor and using GameObject.find or findObjectsOfType =)
– Eddge
Nov 17 '18 at 19:36
Yea, I know I was just thinking it would cool to show another way to do it without having to connect objects in the editor and using GameObject.find or findObjectsOfType =)
– Eddge
Nov 17 '18 at 19:36
1
1
That makes sense. You don't need my approval to do so.
– Programmer
Nov 17 '18 at 19:40
That makes sense. You don't need my approval to do so.
– Programmer
Nov 17 '18 at 19:40
1
1
Amazing answer! Thank you so much!
– Taik
Nov 17 '18 at 19:41
Amazing answer! Thank you so much!
– Taik
Nov 17 '18 at 19:41
add a comment |
If you make Count a static, then each Ball object will share the same value in memory. As someone else pointed-out, if there are more than one of these Ball objects, they would each have their own idea of the value for Count.
static int Count;
Make it static in the class (add one word to your source code) and I think it will act as you desire.
Also, you don't need to explicitly declare 'private' it is the default.
public class Ball : MonoBehaviour {
static int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
add a comment |
If you make Count a static, then each Ball object will share the same value in memory. As someone else pointed-out, if there are more than one of these Ball objects, they would each have their own idea of the value for Count.
static int Count;
Make it static in the class (add one word to your source code) and I think it will act as you desire.
Also, you don't need to explicitly declare 'private' it is the default.
public class Ball : MonoBehaviour {
static int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
add a comment |
If you make Count a static, then each Ball object will share the same value in memory. As someone else pointed-out, if there are more than one of these Ball objects, they would each have their own idea of the value for Count.
static int Count;
Make it static in the class (add one word to your source code) and I think it will act as you desire.
Also, you don't need to explicitly declare 'private' it is the default.
public class Ball : MonoBehaviour {
static int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
If you make Count a static, then each Ball object will share the same value in memory. As someone else pointed-out, if there are more than one of these Ball objects, they would each have their own idea of the value for Count.
static int Count;
Make it static in the class (add one word to your source code) and I think it will act as you desire.
Also, you don't need to explicitly declare 'private' it is the default.
public class Ball : MonoBehaviour {
static int Count;
public Text TextCount;
// Update is called once per frame
Rigidbody rb;
public float destroyTimeOut = 2;
bool hitBase = false;
void Start()
{
rb = GetComponent<Rigidbody>();
Count = 82;
SetTextCount();
}
void Update () {
if(rb.position.y < -3)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
if (hitBase)
{
timer += Time.deltaTime;
if (timer > destroyTimeOut)
{
Destroy(gameObject);
Count = Count - 1;
SetTextCount();
}
}
}
float timer;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("base"))
{
hitBase = true;
}
}
void SetTextCount()
{
TextCount.text = Count.ToString();
}
}
edited Nov 22 '18 at 15:30
answered Nov 20 '18 at 21:08


Jack DraakJack Draak
114
114
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354288%2fwhy-the-score-didnt-work-properly-in-unity-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by "it didn't work properly?"
– Robert Harvey♦
Nov 17 '18 at 18:37
@RobertHarvey I mean the score decrease to 81 and it didn't change even other balls are destroyed
– Taik
Nov 17 '18 at 18:41
I think the problem might be that each ball has its own score.
– Leo Bartkus
Nov 17 '18 at 18:55
Put some logs or use the debugger.
– Iggy
Nov 17 '18 at 19:19
Every ball has a private int for the count that is set to 82, its not a static int or anything so everytime a ball updates the text it is updating it with its own verision of count, which decremented even if on the next frame its personal private count is 81.
– Eddge
Nov 17 '18 at 19:22