Help in unity 2D program

I am creating a Suika game in unity 2d, and I have a problem. I am using the video

. For now I am in the part 2. For now I just can move the cloud, but the fruit are supposed to appear but they don't. I let the two scripts, please someone help.

SCRIPT 1 : Cloudcon

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cloudcon : MonoBehaviour
{
    public Transform[] fruitObj;
    static public string spawnedYet = "N";
    static public Vector2 cloudxPos;

    void Start()
    {

    }

    void Update()
    {

        spawnFruit();

        if (Input.GetKey("a"))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(-2, 0);
        }

        if (Input.GetKey("d"))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(2, 0);
        }

        if ((!Input.GetKey("a")) && (!Input.GetKey("d")))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
        }

        cloudxPos = transform.position;
    }

    void spawnFruit()
    {
        if (spawnedYet=="n")
        {
            StartCoroutine(spawntimer());
            spawnedYet = "y";
        }
    }

    IEnumerator spawntimer()
    {
        yield return new WaitForSeconds(.75f);
        Instantiate(fruitObj[Random.Range(0, 2)], transform.position, fruitObj[0].rotation);
    }
}

SCRIPT 2 : Fruitcon

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fruitcon : MonoBehaviour
{
    private string inthecloud = "y";

    void Start()
    {

    }


    void Update()
    {

        if (inthecloud=="y")
        {
            GetComponent<Transform>().position = Cloudcon.cloudxPos;
        }

        if(Input.GetKeyDown("space"))
        {
            GetComponent<Rigidbody2D>().gravityScale = 1;
            inthecloud = "n";
            Cloudcon.spawnedYet = "n";
        }

    }
}

Sounds like you didn't follow the tutorial, or else you have some other kind of bug.

That means it is time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don't assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step what is going on, otherwise you're just wasting time copying work without learning.

It helps to add Debug.Log statements to your code to see if certain code is being executed. If you include Debug.Log prints along the path to the target function, you can narrow down why it is not running.

You need to be very careful when following tutorials to use the exact same capitalization as the tutorial. You have defined the default value of spawnedYet as "N", but you are checking for "n" in the if statement. This means the code in the if statement never runs.

static public string spawnedYet = "N";

if (spawnedYet=="n")

spawnedYet should probably just be a bool rather than a string.

2 Likes

Thanks for everyone, the problem was the capital N.

Now I have another problem : The game works fine, but when the balls are in the cloud, we don't see it (the cloud is covering the fruit)