I’m trying to make a similar to Fruit ninja game. But when I was making the script of the fruits spawning and then moving across the screen I ran into trouble.
public class PeaScript : MonoBehaviour
{
public Rigidbody2D peaRB;
// Start is called before the first frame update
void Start()
{
peaRB = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
public void MovePea()
{
peaRB.AddForce(Vector2.right * 3, ForceMode2D.Impulse);
}
}
public class Fruit_Spawn : MonoBehaviour
{
public GameObject FruitOne;
public GameObject FruitTwo;
public GameObject FruitThree;
public GameObject FruitFour;
bool one = false;
bool two = false;
bool three = false;
bool four = false;
float oneF = 0f;
float twoF = 0f;
float threeF = 0f;
float fourF = 0f;
public int RoundCount = 0;
private PeaScript PeaScript;
public Rigidbody2D pea;
// Start is called before the first frame update
void Start()
{
NewRound();
FruitOne = GameObject.Find("Pea_vegtable(1)");
}
// Update is called once per frame
void Update()
{
}
void NewRound()
{
oneF = Random.Range(0,2);
twoF = Random.Range(0, 2);
threeF = Random.Range(0, 2);
fourF = Random.Range(0, 2);
Debug.Log(oneF);
if (RoundCount !=5 && oneF==1)
{
PeaScript = FindObjectOfType<PeaScript>();
PeaScript.peaRB = pea;
transform.position = new Vector3(1, 1, 0);
pea.AddForce(Vector2.right * 3, ForceMode2D.Impulse);
}
}
}
I tried to only make a function in pea script to run that would push it but that did not work as it would not let me call it.