Hi, sometimes i think i write my code not in the right way.
For example, i want one of the object to appear slowly:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectAppear : MonoBehaviour {
private float Alpha;
// Use this for initialization
void Start () {
Alpha = 0;
}
// Update is called once per frame
void Update () {
GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alpha);
if(Alpha < 1)
{
Alpha += 0.9f * Time.deltaTime;
}
}
}
So this is the code and its working, But the thing is the code keep repeating himself.
There is any other way to prevent that code from repeating? Any function that destroy the script and not the object.
Thanks!
Donât use âGetComponentâ every frame, cache it in Awake and then reference to it.
You can simply use Destroy(this); to remove just script if you wonât use it againâŚ
My script would probably look something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectAppear : MonoBehaviour
{
// Alpha to start with
private float Alpha = 0;
// Reference to our SpriteRenderer, fetched once with GetComponent in Awake
private SpriteRenderer spriteRenderer;
// bool so this script knows if it still needs to update the alpha of the sprite
private bool isTransitioningAlpha = true;
void Awake()
{
// Get the SpriteRenderer attached to this GameObject
spriteRenderer = GetComponent<SpriteRenderer>();
if(spriteRenderer == null)
Debug.LogError("GameObject does not have a SpriteRenderer component!", this);
}
void Update ()
{
// If we are at max alpha, we do not need to continue to assign a new Color.
// Its not changing
if(isTransitioningAlpha == false)
return;
// Alpha is not max yet, increment it some amount and assign it to the spriterenderer
if(Alpha < 1)
{
Alpha += 0.9f * Time.deltaTime;
spriteRenderer.color = new Color(1, 1, 1, Alpha);
}
else
{
// Alpha is greater than or equal to 1. Transition is finished
isTransitioningAlpha = false;
// ALTERNATIVELY:
// This could be where you destroy this component. Which would probably be better.
}
}
}
This is so we are not continually assigning a new color to the spriterenderer unless the alpha has changed
I think the answer was to use IEnumerator ?
I just want my game to run faster, i need to avoid all those codes that i dont need.
I needed the Function just once, but as you see the script ask the question again and again until the object destroyed.
What i wanted is to remove just the Script Component.
Destroy(this) is destroying also the object no?
Edit:
this was the answer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectAppear : MonoBehaviour {
private float Alpha;
// Use this for initialization
void Start () {
Alpha = 0;
}
// Update is called once per frame
void Update () {
GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alpha);
if (Alpha < 1)
{
Alpha += 0.9f * Time.deltaTime;
}
else Destroy(GetComponent<ObjectAppear>());
}
}
Nope. âthisâ refers to the script instance, so only that will get Destroyed. Destroy(gameObject) is what destroys the GameObject the script is attached to.
I get the impression your guessing⌠donât guess. Find out what elements are causing any slow down you have, then work on those elements. Since this is a really simple script I doubt it causing you that much bother.
You are still calling GetComponent every frame, dont. Call it once in Awake and cache it. This is the single most intense part in the script. (Relative of course, theres not a lot else going on)