I read the book for beginners and constructed the following script.
When the main character (Uta) is hit, the hero disappears, an alternative sprite is displayed, and it is an action of being pulled upward.
Although it operates, the image specified by will not be displayed. The main character disappears and becomes inactive, and it is seen that the enemy is going away on top. However, the image set on the inspector is not displayed.
Advice please.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BruburuScript : MonoBehaviour {
public Sprite BruSprite;
bool isBru = false;
// Use this for initialization
void Start () {
Vector3 pos = this.transform.position;
pos.y = Random.value * 4 + 1;
this.transform.position = pos;
}
// Update is called once per frame
void Update () {
if(isBru){
Vector3 pos = this.transform.position;
pos.y += Time.deltaTime * 6;
this.transform.position = pos;
}
}
void OnTriggerEnter2D (Collider2D col)
{
if(!col .gameObject.name.Equals("Uta")){
return;
}
col.gameObject.SetActive(false);
this.GetComponent<SpriteRenderer>().sprite = BruSprite;
isBru = true;
}
}