activeSelf返回true,而gameobject为false [英] activeSelf return true and gameobject is false
问题描述
所以我有一个名为menuView
的游戏对象.我创建了toogleGameobject
脚本,它很简单-检查它是否为selfActive
,然后将其设置为false,然后将其设置为true.问题是由于某种原因它无法正常工作.然后在该函数中设置Debug.Log(selfActive)
,并在控制台中返回它为true,但我的游戏对象为false.
So i have gameobject called menuView
. I created script that toogleGameobject
and it is simple - check if it is selfActive
, if it is then set to false and if it false then set it to true. Problem is that for some reason it was not working. Then inside that function i set Debug.Log(selfActive)
and in my console it returns that it is true but my gameobject is false.
这是图片:
我正在通过按钮调用脚本,并且脚本需要参数gameObject
,所以我通过检查器对其进行了分配.
I am calling script by button and script need parameter gameObject
so I assign it through inspector.
public void toogleGameObject(GameObject gameobject)
{
Debug.Log(gameobject + " " + gameObject.activeSelf);
//In image above this down was under comment, so only Debug.Log was caled with function
if(gameObject.activeSelf == true)
{
gameObject.SetActive(false);
}
else
{
gameObject.SetActive(true);
}
}
在这里,我分配并调用游戏对象:
Here i assign and call gameobject:
推荐答案
请注意如何命名变量.从MonoBehaviour
和Component
继承的本地变量名为"gameObject
".
Be careful how you name your variables. There is a local variable inherited from MonoBehaviour
and Component
named "gameObject
".
您使用gameObject
来引用此脚本附加到的GameObject.
You use that gameObject
to refer to the GameObject that this script is attached to.
脚本附加到的GameObject是您当前在/ff上切换的对象,而不是传递给toogleGameObject
函数的对象.
That GameObject the script is attached to is what you are currently toggling on/ff not the one that is passed to the toogleGameObject
function.
传递给toogleGameObject函数的GameObject命名为gameobject
而不是gameObject
. O 不大写.
The GameObject that is passed to the toogleGameObject function is named gameobject
not gameObject
.The O is not capitalized.
public void toogleGameObject(GameObject gameobject)
{
Debug.Log(gameobject + " " + gameobject.activeSelf);
//In image above this down was under comment, so only Debug.Log was caled with function
if(gameobject.activeSelf == true)
{
gameobject.SetActive(false);
}
else
{
gameobject.SetActive(true);
}
}
您还可以将其简化为:
public void toogleGameObject(GameObject gameobject)
{
Debug.Log(gameobject + " " + gameobject.activeSelf);
gameobject.SetActive(!gameobject.activeSelf);
}
最后,我建议您将参数变量GameObject gameobject
重命名为GameObject objToToggle
,以免将来再次犯此错误.
Finally, I suggest you rename the parameter variable GameObject gameobject
to GameObject objToToggle
so that you won't make this mistake in the future again.
public void toogleGameObject(GameObject objToToggle)
{
Debug.Log(objToToggle + " " + objToToggle.activeSelf);
objToToggle.SetActive(!objToToggle.activeSelf);
}
这篇关于activeSelf返回true,而gameobject为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!