我的预制件有一个脚本,但是,每次生成时,该脚本都被禁用。我尝试了GetComponent()。enable = true(不起作用),我附加了图像->(Prefab),这是我的代码。 FYI预制件使用默认的UNET LobbyManager生成。
LobbyManager生成预制件,我使用一个单独的脚本来控制预制件。该脚本已附加到预制件上,但是在生成时会丢失,因此我一直在寻找预制件,并尝试通过代码附加它。
GameObject imageTracker_1, imageTracker_2;
public GameObject[] user;
public GameObject myTextObj, origin, canvasUI;
public float distanceFromOrigin;
//public Text myText;
private void Awake()
{
user[0] = GameObject.FindWithTag("Respawn");
user[1] = GameObject.FindWithTag("Finish");
}
void Start()
{
if (!isLocalPlayer)
{
Destroy(this);
return;
}
origin = GameObject.Find("PositionOrigin");
//used for finding text, "GetComponent<Text>" needs to be attached or fails.
canvasUI = GameObject.Find("Canvas");
//myText = canvasUI.GetComponentInChildren<Text>();
// myText.text = "Out of area";
myTextObj.SetActive(false);
// we track the vuforia image within the scene and attach the avatar as a child
//imageTracker_1 = GameObject.FindWithTag("Respawn");
//GameObject imageTracker_2 = GameObject.FindWithTag("Finish");
//transform.SetParent(imageTracker_1.transform, false);
//transform.parent = imageTracker_1.transform;
}
private void Update()
{
assigningAvatarToParent();
distanceFromOrigin = Vector3.Distance(origin.transform.position, transform.position);
//during start the gameObject will return NULL, this will make sure during every frame the gameObject does not equal null
if (origin || imageTracker_1 || myTextObj == null)
{
myTextObj = GameObject.Find("OutOfBound");
origin = GameObject.Find("PositionOrigin");
}
}
void assigningAvatarToParent()
{
int k = 0;
while (k < user.Length){
GameObject avatar = GameObject.Find("player_avatar" + k);
avatar.transform.SetParent(user[k].transform, false);
k++;
Debug.Log("avatar for parents: " + avatar);
}
for (int a = 0; a < user.Length; a++)
{
GameObject avatar = GameObject.Find("player_avatar" + a);
if (avatar.GetComponent<playerIdentity>() == null){
avatar.AddComponent<playerIdentity>();
origin = GameObject.Find("PositionOrigin");
canvasUI = GameObject.Find("Canvas");
myTextObj.SetActive(false);
}
}
return;
}
}
答案 0 :(得分:1)
我假设您的脚本是从class Test < ActiveModel::Serializer
attributes :a, :b, :c
end
s = Test.new(Object)
s.a
派生的-禁用了afaik,直到与服务器的活动连接为止。这可能是问题吗? (请参见UNet Documentation)
此外,在实例化Prefab时,您的脚本会在NetworkBehaviour
函数中从中删除自身。这就是为什么它不见了。
调用Start()
会删除脚本组件,但不会删除Destroy(this);
(而且我自己陷入该陷阱的次数约为100次)。
不确定这是否是您的意图,因为LobbyManager正在创建您的预制件。但是,如果是这样,则可能要改用GameObject
。