简体   繁体   English

如何让游戏对象将更改应用于脚本中的预制件

[英]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.

相关问题 如何将公共脚本添加到 Prefab? - How do I add a public script to Prefab? 如何拥有多个精灵的游戏 object 预制件? - How to have a game object prefab with mutiple sprites? 如何将预制件作为目标分配给 CameraControl 脚本? - How do I assign a prefab as a target to a CameraControl script? Unity-如何将动画从一个游戏对象应用于另一个装备的游戏对象? - Unity - How do I apply an animation from one game object to another rigged game object? 销毁预制件中的物体后,如何重新实例化该预制件? - How do I re-instantiate original prefab after an object in that prefab has been destroyed? 我如何在我刚刚实例化的预制组件中引用场景中的游戏 object? - How can I reference a game object on my scene in a prefab component I have just instantiated? 如何从预制件中获取场景中游戏对象的参考 - How can I grab a reference of a game-object in a scene from a prefab 我如何使游戏对象跟随另一个游戏对象但不移动到目标对象中? - How do i get the game object to follow another game object but not move into the targeted object? 当游戏对象与主角碰撞时,如何破坏整个预制件? - How to destroy the whole prefab when the game object collides with the main character? 如何将游戏对象从层次结构设置为预制件。 统一 - How to set a game object from hierarchy to a prefab. unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM