- Home /
Issue solved by forum and by tutorial. App is working as expected.
Ninja fruit - code correct but bomb missing
Updated: I have reviewed part of the tutorial but still cannot get the bomb to appear when play is clicked. To avoid long unnecessary stories, I am putting a screenshot of what the tutorial show and what I have. The codes (scripts) have been left out as I won't know what you experts want to check. You can request for the entire project if you have time. I have fixed 2019.2.0 so that although error messages appear on the status, it doesn't affect the app at all. I've tested other apps and successfully created the .apk installation files and everything works well. Please see the screenshot of the bomb - shown in white box and my screen where the fruit spawner has the correct items filled. What do i need to check or edit?
alt text
share the errors appearing and the spawner code, if you are sure the code is fine then the issue is with the errors, even if you play the game if there are errors app may not work as expected
Hello, @JulianHSC ! In what script bomb is missing? Check that attribute in inspector. If it is filled, then I suggest you to set break point at line, where bomb is missing and debug it. If in debug mode that value is really empty, then you clear it somewhere in code maybe?
I don't know how to use debug. I'm still quite new to Unity. I would be happier if someone can just check what's wrong with the code OR if the player is the problem.
add to all of the methods Debug.Log("$$anonymous$$ethod name"); changing the method name for the actual method name, like Debug.Log("StartSpawning"); and tell us if all are getting printed right, share the errors that appear in the console
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FruitSpawner : $$anonymous$$onoBehaviour
{
public GameObject fruit;
public GameObject bomb2;
public float maxX;
// Start is called before the first frame update
void Start()
{
Invoke("StartSpawning", 1f);
}
// Update is called once per frame
void Update()
{
}
public void StartSpawning()
{
InvokeRepeating("SpawnFruitGroups", 1, 6f);
}
public void StopSpawning()
{
CancelInvoke("SpawnFruitGroups");
StopCoroutine("SpawnFruit");
}
void SpawnBomb()
{
float Rand = Random.Range(-maxX, maxX);
Vector3 pos = new Vector3(Rand, transform.position.y, 0);
GameObject b = Instantiate(fruit, pos, Quaternion.identity) as GameObject;
b.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 15f), Force$$anonymous$$ode2D.Impulse);
b.GetComponent<Rigidbody2D>().AddTorque(Random.Range(-50f, 50f));
}
public void SpawnFruitGroups()
{
StartCoroutine("SpawnFruit");
if (Random.Range(0,6) > 4)
{
SpawnBomb();
}
}
IEnumerator SpawnFruit()
{
for (int i = 0; i < 5; i++)
{
float Rand = Random.Range(-maxX, maxX);
Vector3 pos = new Vector3(Rand, transform.position.y, 0);
GameObject f = Instantiate(fruit, pos, Quaternion.identity) as GameObject;
f.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 15f), Force$$anonymous$$ode2D.Impulse);
f.GetComponent<Rigidbody2D>().AddTorque(Random.Range(-50f, 50f));
yield return new WaitForSeconds(0.5f);
}
}
}
Answer by Kennai · Aug 09, 2019 at 12:24 PM
in function SpawnBomb you spawn fruit,not bomb.
GameObject b = Instantiate(fruit, pos, Quaternion.identity) as GameObject;
replace fruit with bomb2.
his issue is most likely this " I have fixed 2019.2.0 so that although error messages appear on the status, it doesn't affect the app at all" in the screenshot he shared not even bombs and fruits get iinstantiated. the errors are most likely affecting the gameplay
Thank you. Your code worked. But i still had to play around with the object in prefab and outside. Now both my fruit and bomb does show. However, the bomb rarely appears. But still your code at least solved it. Earlier on, the bomb just refused to appear. But now I'm wondering : is there a way to make the bomb appear more often - i mean without adding much code to it?
look at function SpawnFruitGroups
there is line if (Random.Range(0,6) > 4)
so it generates random value between 0..5 and if it is greater than 4 - it creates bomb.
if you want increase appearing - just decrease value 4. like that: if (Random.Range(0,6) > 2)
Yes, I just checked the tutorial again and that's the answer. Thanks for your great help. This case has been pending for a few weeks / months. I think you solved the biggest problem I had in the entire course. Now I can take a short vacation before continuing my course.
Follow this Question
Related Questions
Standalond built for Windows doesn't include text assets 0 Answers
The 3 mono scripts that come with the First Person Controller asset are missing! 1 Answer
Creating a Prefab and Keeping Original Objects Script Information 0 Answers
Checking to see if a Public transform is (missing) 1 Answer
Door Missing 1 Answer