[英]how do i get a game object to apply changes to a prefab in script
Using the Unity 3D editor. 使用Unity 3D编辑器。 I have an object which changes colour when it collides with other objects, this all works, but when changing scene the ball reverts back to its default colour. 我有一个物体在与其他物体碰撞时会改变颜色,这一切都有效,但是当改变场景时,球会恢复到默认颜色。 So i need the ball when changing colour to update the prefab through the script so the other instances of the object in other scenes will also change colour. 所以我在改变颜色时需要球来通过脚本更新预制件,这样其他场景中对象的其他实例也会改变颜色。 here is the code i have written for the colour changing: 这是我为变色编写的代码:
using UnityEngine;
using System.Collections;
public class colourpicker : MonoBehaviour {
public GameObject whiteblock;
public GameObject limegreentintblock;
public GameObject cyantintblock;
public GameObject redtintblock;
public GameObject yellowtintblock;
public GameObject player;
public Material limegreentint;
public Material redtint;
public Material cyantint;
public Material yellowtint;
public Material white;
void OnTriggerEnter(Collider other)
{
if (other.gameObject == whiteblock)
{
renderer.material = white;
}
else if (other.gameObject == redtintblock)
{
renderer.material = redtint;
}
else if (other.gameObject == limegreentintblock)
{
renderer.material = limegreentint;
}
else if (other.gameObject == cyantintblock)
{
renderer.material = cyantint;
}
else if (other.gameObject == yellowtintblock)
{
renderer.material = yellowtint;
}
}
}
thanks for any help in advance :) 谢谢你提前帮助:)
If I were you I'd make a static class with an Empty Game Object where you can save the state of your object colour. 如果我是你,我会创建一个带有空游戏对象的静态类,您可以在其中保存对象颜色的状态。 In the Initialize part of you Ball you should check if a variable (that must be static as well) of this static class is different from an Initial state and then assign the colour of the static variable to the Ball object. 在Ball的Initialize部分中,您应该检查此静态类的变量(必须是静态的)是否与Initial状态不同,然后将静态变量的颜色分配给Ball对象。 This LINK teaches you how to implement it (you have to change some stuff to make what you want achieve) 这个LINK教你如何实现它(你必须改变一些东西来做你想要的东西)
EDIT 编辑
In the static class you create something like 在静态类中,您可以创建类似的东西
public static Material BallMaterial = null; // or you can assign a Material that will act as InitialState
After that, in your piece of code, when you assign the new Material to the ball, you also assign the new material to BallMaterial and then when you load the new scene you check if the BallMaterial has a different value from its InitialState (or null). 之后,在您的代码中,当您将新材质指定给球时,您还将新材质指定给BallMaterial ,然后在加载新场景时检查BallMaterial是否具有与其InitialState不同的值(或null )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.