the image set on the inspector is not displayed. I am a beginner
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;
}
}
Please, edit your question to provide the english translation. Unity Answers is an English-speaking website, used by thousands of developpers. Not all of them speak your native language.
Good day.
This is an international forum, please remake your question in english so we can understand you and you can get an answer....
Bye.
Answer by tormentoarmagedoom · Jun 21, 2018 at 03:55 PM
Good day.
Is possible you have a Space here?
if(!col .gameObject.name.Equals("Uta")){
right after .gameobject?
General Tips: As you say you are a begginer, take note:
If want to ferear the object that contains this script, use "gameObject" (minusc "g") , not "this" like here:
gameObject.transform.position = pos;
[is the object who have a transform, not the script]
[...]
or...
gameObject.GetComponent().sprite = BruSprite;
[is the gameObject who have components. The script is a Component of the gameObject also]
Bye!
Answer by Hide_pixel · Jun 21, 2018 at 11:58 PM
tormentoarmagedoom
Thank you! But... No picture appears like the picture.!
[I changed "BruSprite" to the same name "hookOnSprite" as book. Copy & pasete]
I changed the code as follows. Thanking you in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BruburuScript : MonoBehaviour
{
public Sprite hookOnSprite;
bool isHook = false;
void Start()
{
Vector3 pos = this.transform.position;
pos.y = Random.value * 4 + 1;
this.transform.position = pos;
}
void Update()
{
if (isHook)
{
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);
GetComponent<SpriteRenderer>().sprite=hookOnSprite;
isHook = true;
}
}
Your answer
Follow this Question
Related Questions
Get sprite size and position 0 Answers
Sprite not properly updating color 0 Answers
Is it possible to merge two sprites? 0 Answers
Instantiate prefab with its component c# 0 Answers