issues with other.gameObject.SetActive(false);
I'm following the Space Shooter tutorial but I'm trying to change the script for the explosions to just collecting the Asteroid. I have no errors in the script but the Asteroid does not disappear with it Collides with my spaceship and the score keeps adding points soon as an Asteroid comes into frame.
If anyone can provide me a hint on where I am wrong please do let me know!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Collect: MonoBehaviour {
public int scoreValue;
private Done_GameController gameController;
void Start ()
{
GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <Done_GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter (Collider other)
{
//Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
if (other.gameObject.CompareTag("Enemy"))
//... then set the other object we just collided with to inactive.
other.gameObject.SetActive(false);
gameController.AddScore(scoreValue);
//Add one to the current value of our count variable.
}
}
I think you have missed the braces under the if
statement.
if (other.gameObject.CompareTag("Enemy"))
{ // here
//... then set the other object we just collided with to inactive.
other.gameObject.SetActive(false);
//Add one to the current value of our count variable.
gameController.AddScore(scoreValue);
} // here
Also, make sure the other gameObject is correctly tagged Enemy
in the editor
Answer by tormentoarmagedoom · Sep 05, 2018 at 09:13 AM
Good day.
You need to detect "strange things".
Look at line 25, you see the code is moved to right? this is because there is something strange, your scripting program detects something is wrong with "squematic" symbols like ( ) or { } or [ ] ..
So if you check it will see your if sentence from line 20 have nothing inside, because you did not put any "{...}"
And the code thinks the lines 24 and 25 are not inside that if...
Good bye!
Yes, the if
statement contains the statement line 24. The comment is not taken into account by the compiler, nor the identation.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174102im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cycle through objects on mouse click 1 Answer
GameObject - SetActive not working 1 Answer
Script in C sharp collision when entered fires arrows at player 0 Answers
Unity - creating a breakout style game - ball will not destroy blocks or be reset 0 Answers
I got a bunch of problems and i don't know how to fix them! 0 Answers