How to make multiple game object work with the same scripts?
Hello everyone, this is my second project in Unity so I'm very noob at this and desperately looking for help.
I'm trying to make a game where different type of enemies will be coming to the screen and when one of the enemy game object is clicked, it will change it's direction and start moving towards the y axis.
My script works fine for the first enemy I made. However, I created another Object for a second type of enemy. And I used the same scripts for it but this game object isn't responding to the scripts. Please someone tell me how to fix this.
This is my code for the enemy to be moving towards the x axis. When I click on the enemy, this script is supposed to disable and enable another script which makes the enemy move in the y axis direction. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AlphabetMove : MonoBehaviour
{ private float AlphaSpeed =3;
private bool checker =false;
// Start is called before the first frame update
void Start()
{
this.checker = false;
}
// Update is called once per frame
void Update()
{
this.transform.position = new Vector2(this.transform.position.x - AlphaSpeed*Time.deltaTime, this.transform.position.y);
}
void OnMouseDown ()
{
if(this.checker==false)
{this.checker=true;
this.enabled = false;
this.gameObject.GetComponent<AlphabetMoveDown>().enabled = true;
}
}
}
This is the code which is supposed to make the enemy move in the y axis. This code will be enabled once the object is clicked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AlphabetMoveDown : MonoBehaviour
{ private float Alpha =3;
// Start is called before the first frame update
void Start()
{
this.enabled = false;
}
// Update is called once per frame
void Update()
{
this.transform.position = new Vector2(this.transform.position.x ,this.transform.position.y - Alpha*Time.deltaTime);
if(this.transform.position.y<=-6.1)
{
Debug.Log("Game Over");
}
}
}
My second type of enemy is moving in the x axis direction but nothing is happening when I click on it, any time of help is appreciated! Thanks in advance.