Unity变量重置为零 C# Unity

huangapple go评论46阅读模式
英文:

Unity variables reset to zero C# Unity

问题

The value of 'defaultScale' doesn't change. It seems like the 'BackToOriginalSize' function can't access the value.

'value'默认值没有改变。似乎'BackToOriginalSize'函数无法访问该值。

英文:

I have the following code:

    private float defaultScale;

    public void Start()
    {
        defaultScale = (float)this.transform.localScale.x;
    }

The value of defaultScale can be printed in other functions such as

    public void increaseSize()
    {
        Debug.Log("Increase Size: " + defaultScale);
        if(!unlocked){
            return;
        }
        GameObject gameObj = GameObject.FindGameObjectsWithTag("medical")[0];
        gameObj.transform.localScale += specification/scaling;
    }

But it will reset to zero everytime when I call this function:

    public void BackToOriginalSize() {
        Debug.Log("Back To Original Size: " + defaultScale);
        if(!unlocked) {
            return;
        }
        GameObject gameObj = GameObject.FindGameObjectsWithTag("medical")[0];
        gameObj.transform.localScale *= (defaultScale/gameObj.transform.localScale.x);
        
    }

The actual value of 'defaultScale' doesn't change. It is just somehow BackToOriginalSize function can't access the value.

I got rid of the last two lines of BackToOriginalSize function and also tried calling it at different times, but all my efforts were unsuccessful.

I was wondering what's wrong with my code.

答案1

得分: 2

我不太理解计算原理或为什么首先存储对象的原始比例 (this.transform.localScale),而后通过 FindGameObjectsWithTag 遍历 .. 或者为什么如果你只想要第一个对象,你还要使用多个对象池。

如果这些不在同一个类中,那么 defaultScale 可能根本没有被初始化,保持其默认值 0

我会简单地做像这样的事情:

private GameObject medical;
private Vector3 originalScale;

private void Start()
{
    medical = GameObject.FindWithTag("medical");
    originalScale = current.transform.localScale;
}

public void increaseSize()
{
    if(!unlocked) return;

    medical.transform.localScale += specification/scaling;
}

public void BackToOriginalSize() 
{

    if(!unlocked) return;


    medical.transform.localScale = originalScale;
}

尽管一般来说,这可能更适合在 medical 对象本身的一个组件中发生。

英文:

I don't really get the calculation or why you first store the original scale of this object (this.transform.localScale) but later go through FindGameObjectsWithTag .. or why you use the multiple objects poll at all if you only want the very first one anyway.

If those aren't in the same class then defaultScale might simply be never initialized and keep its default value 0.

I would simply do something like e.g.

private GameObject medical;
private Vector3 originalScale;

private void Start()
{
    medical = GameObject.FindWithTag("medical");
    originalScale = current.transform.localScale;
}

public void increaseSize()
{
    if(!unlocked) return;
      
    medical.transform.localScale += specification/scaling;
}

public void BackToOriginalSize() 
{
    
    if(!unlocked) return;
        
    
    medical.transform.localScale = originalScale;
}

Though in general this is probably rather something that should happen in a component of the medical object itself

huangapple
  • 本文由 发表于 2023年5月11日 00:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定