- Home /
If score spawn objects
If someone scores i want an object to spawn if i use += 1; it says int cant convert into bool if i use <= 1; it just keeps spawning
using System.Collections;
public class Ifmove : MonoBehaviour {
public GameObject Row;
// Update is called once per frame
void Update () {
if(PointScore.pointScore += 1){
Instantiate(Row, transform.position, Quaternion.identity);
}
}
}
please help thank you
Answer by Deathdefy · Feb 10, 2015 at 12:46 AM
This should solve your issue.
void Update () {
//Assuming PointScore.pointScore is 0, this will fire off once and never again after that
PointScore.pointScore++;
if(PointScore.pointScore <= 1){
Instantiate(Row, transform.position, Quaternion.identity);
}
}
The issue you experienced was the fact that you were attempting to check an integer as a bool as you are using `if(PointScore.pointScore += 1) which is not a valid expression. So with the solution I provided pointscore is now only going to resolve as true if it is less than or equal to 1
I tried your method but my prefab just keeps spawning, it doesn't stop
Can you post all of the code you are currently using for me? Unless you are editing pointScore's value somewhere else this should only run 1 time.
Well i have 3 scripts using the code. First one is just the score with a simple gui using UnityEngine; using System.Collections;
public class PointScore : $$anonymous$$onoBehaviour { public static int addScore; void Start(){
PointScore.addScore = 0;
}
void OnGUI ()
{
GUI.Label (new Rect (10, 10, 100, 20), "Score: " + addScore);
}
}
The second one is a trigger that just detect my square at a certain position and if you get it correctly you add a score using UnityEngine; using System.Collections;
public class Thetrigger : $$anonymous$$onoBehaviour {
public bool triggered;
void Update(){
if (triggered == true) {
if(Input.Get$$anonymous$$ouseButtonDown(0)){
PointScore.addScore += 1;
}
}
}
void OnTriggerEnter2D(Collider2D coll){
if (coll.gameObject.tag == "red"){
triggered = true;
}
}
void OnTriggerExit2D(Collider2D coll){
if (coll.gameObject.tag == "red") {
triggered = false;
}
}
}
The Last one is just the spawner that moves up after it spawns my prefab
using UnityEngine; using System.Collections;
public class Spawn$$anonymous$$over : $$anonymous$$onoBehaviour {
public GameObject daprefab;
// Update is called once per frame
void Update () {
if(PointScore.addScore <= 1){
Instantiate(daprefab,transform.position,Quaternion.identity);
transform.Translate(Vector3.up * 6);
}
} }
Im trying to make a simple stacker game, for my own solo project to get to know unity.
Your answer
Follow this Question
Related Questions
if statement not working 1 Answer
Call function every time the player gets 5 points 1 Answer
If statement not catching certain parameters? 2 Answers
Having camera attack upon spawning player? 1 Answer
ai spawning please help 0 Answers