Desc:
There is a component with list data:
public class TestPrefab : MonoBehaviour
{
public List<string> DataList;
}
Then I create a prefab with this component like this:
Then I drag the prefab into scene, modify the component data like this ("Data1"=>"Data1_Modified") (I wont apply changes because I just wanna keep it as a prefab instance, which has some changes from the origin prefab):
Then back to the prefab stage, delete the first data:
Then the prefab instance in the scene comes to:
How does it happen?
unity save this modification like this:
so unity just change the first data in the list to "Data1_Modified", rather than change "Data1" to "Data1_Modified".
Question:
Is there any elegant method to avoid this unpredicted behavior?
you should apply this edits on prefabs so after you finish in inspector menu press on overrides then apply all
prefabs are prefabricated objects so to update them from Scene window you need to tell editor to apply your changes on prefabs.
Related
I want to create a blur effect similar to the picture below:
the picture is taken from this site:
https://forum.unity.com/threads/how-to-blur-specific-layers-only.555520/
I tried the post-processing profile and played around with the depth of field in the post-processing volume but it blurs all the scene.
I came across a YouTube Video that explains how to implement similar results to what I am looking for but things have changed drastically in the settings. For example, at 1:57 (minute and 57 seconds) he clicks on Add Additional Camera Data which I am struggling to find in the latest versions of LWRP.
I am using Unity version 2019.2.9f1
How can I achieve the result in the picture above? (Blurring all the scene except for one object)
Your guidance will be appreciated.
NOTE: My project is in VR using SteamVR and VRTK
Though your question is a bit broad I took some time to show you how it can be done.
First of all you will need to import the Post Processing package via the PackageManager
Window → PackageManager
Make sure to be in the All Packages view, search for post, find the Post Processing package and hit Install
Now first of all go to the Layer settings (Layers → Edit Layers)
and add two additional Layers: e.g. PostProcessing and Focused
Now to the cameras. Afaik it makes no difference whether you are in VR or not, usually you have one MainCamera that is moved along with your headset. If there should be two of them in your project setup just repeat the same steps for the second camera.
Make sure the MainCamera doesn't render the two added Layers → remove them from the Culling Mask
Add a new Camera FocusCamera as child to the existing MainCamera. This way it is automatically moved along with the main Camera.
RightClick on MainCamera &rightarrow Camera
It should have all the settings equal to the MainCamera except:
Clear Flags : Don't Clear
If you set it to Depth Only the focused object will always be rendered on top of everything, even if it is actually behind other objects in 3D space. You decide which effect you want here ;)
CullingMask : only Focused
Depth : Anything higher than the MainCamera so this camera is rendered on top of it
Make sure to remove the AudioListener component.
Finally add a new PostProcessingVolume to the scene. I would add it as child to the FocusCamera! Why? - Because this way it is automatically disabled together with the FocusCamera!
RightClick on FocusCamera → 3D Object → Post Processing Volume
Set its Layer to the added PostProcessing
enable Is Global so the distance to the volume doesn't matter and add a new profile by hitting new → Unity → Depth of field
In your case you want to overwrite the Focus Distance so check the box on the left and set a value close to the camera like e.g. 0.5
Until now nothing has really changed in your scene.
Now go to the MainCamera and, a component PostProcessingLayer and set the Layer to our added layer PostProcessing
Now everything should be blurred in your scene!
Almost ready to go! Now Disable the FocusCamera and add this script to it
using UnityEngine;
public class FocusSwitcher : MonoBehaviour
{
public string FocusedLayer = "Focused";
private GameObject currentlyFocused;
private int previousLayer;
public void SetFocused(GameObject obj)
{
// enables this camera and the postProcessingVolume which is the child
gameObject.SetActive(true);
// if something else was focused before reset it
if (currentlyFocused) currentlyFocused.layer = previousLayer;
// store and focus the new object
currentlyFocused = obj;
if (currentlyFocused)
{
previousLayer = currentlyFocused.layer;
currentlyFocused.layer = LayerMask.NameToLayer(FocusedLayer);
}
else
{
// if no object is focused disable the FocusCamera
// and PostProcessingVolume for not wasting rendering resources
gameObject.SetActive(false);
}
}
// On disable make sure to reset the current object
private void OnDisable()
{
if (currentlyFocused) currentlyFocused.layer =previousLayer;
currentlyFocused = null;
}
}
This will allow you to focus a certain GameObject on runtime by changing its layer to the Focused layer we added, the only one that is rendered by the FocusCamera. So this object will be rendered on top of the image without any blur effect!
For demonstration I just added this simple script to every cube object in order to enable focus on mouse enter and disable it on mouse exit:
using UnityEngine;
public class FocusMe : MonoBehaviour
{
[SerializeField] private FocusSwitcher focus;
private void OnMouseEnter()
{
focus.SetFocused(gameObject);
}
private void OnMouseExit()
{
// reset the focus
// in the future you should maybe check first
// if this object is actually the focused one currently
focus.SetFocused(null);
}
}
And here is what it looks like
as said I don't know exactly what your VR setup looks like. If you have to MainCameras simply add two child cameras to them. You still will need only one PostProcessingVolume and only one FocusSwitcher so you would probably move them to another object and handle the camera disabling etc differently but I hope the idea gets clear enough.
Use a separate camera for objects you don't want to blur and set a higher depth value.
Set the ClearFlags to depth only and in the CullingMask select the layer of that one object(or more objects). Obviously you would require to have a different layer for unblurred objects.
I am dynamically loading GameObjects from a JSON file into the Scene.
I am able to instantiate GameObjects from a prefab, but facing issue changing the RigidBody2d properties.
Instantiation
GameObject greenObj = Instantiate(rotateBouncerObject,
new Vector3(gamObj.position.x, gamObj.position.y, 0),
Quaternion.Euler(gamObj.rotation.eulerAngles.x,
gamObj.rotation.eulerAngles.x, gamObj.rotation.eulerAngles.z));
The problem I am facing is that when I change the property of RigidBody2D like bodyType it doesn't change. I am changing the bodyType using the code from Kinematic to Dynamic like this.
greenObj.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic.
When I check in Unity Editor the body type is not updated, but when I log the bodyType it gives me changed bodyType.
Need your inputs on this.
I have a player prefab that is spawned when the game starts via Network Manager, the player prefab has a script(Player Health) attached to it which takes 2 UI element as input(DamageImage, HealthSlider) present in the hierarchy.The problem i face is that I cant add these inputs in the prefab which results in Object reference not set to an instance of an object error.
Tried to add them by placing the prefab in hierarchy and then saving it but it did not help.
If the UI elements aren't in the prefab itself or in one of its children then you will lose the reference once you make the prefab.
What you can do is set the tag of those elements to something like "DamageImage" and "HealthSlider" and then in the Awake() function of a your PlayerHealth script attached to your prefab do this:
void Awake(){
DamageImage = GameObject.FindWithTag("DamageImage").GetComponent<Image>();
HealthSlider = GameObject.FindWithTag("HealthSlider").GetComponent<Slider>();
}
Okay so I've been making a 2d platformer and had a terrible image/sprite for my player. Now I've got a better one and want to just replace the images but keep all the same values and data/scripts etc.
I've been trying to figure it out for awhile but to no avail. Thanks for any help
If you want to permanently change a sprite on your prefabs/objects you can drag the new sprite from your asset folder to into the "Sprite Renderer" Component of the object you want to change, replacing whatever is currently in the sprite box. Check out this image to see exactly where you want to drag the sprite:
O you can change it via script using a public variable:
public class ChangeSprite: MonoBehaviour
{
public Sprite newSprite;
private void ChangeSprite(){
gameObject.GetComponent<SpriteRenderer>().sprite == newSprite;
}
}
One way to do it, not sure it is the best way but it works, is to:
- import your new Sprite (let's call it SpriteB)
- select the GameObject where you have been using your first sprite (SpriteA)
- in the "Sprite Renderer" component of your selected GameObject, replace "Sprite=SpriteA" with your new sprite so that "Sprite=SpriteB"
Obviously you will have to repeat the operation for every GameObject where you may have used SpriteA.
I am working on a FPS game. I which my first scene in Main menu scene. Which is fine. In the second scene user is selecting the gun which he want to use (There are four guns). When he press play i am loading the new scene(gamePlay). How can i keep track of the gun which the user selected? so, he can use that gun in the gamePlay? its easy when you are working in a single scene by switching your camera. But how to do it in the new scene?
This is how I have been doing and it works really well.
Implement a Singleton that derives from MonoBehaviour to hold user choices between menus and that will not be destroyed when a new level is loaded.
Singleton example
public class UserChoices : MonoBehaviour
{
public static UserChoices Instance = null;
// just an example, add more as you like
public int gun;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
if(Instance == null)
{
Instance = this;
} else if(Instance != this)
{
Destroy(this.gameObject);
}
}
}
Now you just need to do this once:
Create an empty GameObject
Attach this script to it and then save it as a prefab
Drag it too every scene that needs it
Now you can save and read the user choices between scenes easily:
// read chosen gun
int chosen_gun = UserChoises.Intance.gun;
//save chosen gun
UserChoises.Intance.gun = PlayerGuns.Knife;
Just a side note, I usually create properties instead of accessing simply public variables because I like to control what values are assigned (validation, extra behaviors, etc...)
References:
MonoBehaviour
DontDestroyOnLoad
You can set the GameObjects that the user is selecting to not be automatically destroyed. Take a look at DontDestroyOnLoad.
This way they get preserved when changing scenes.
You'll probably need to be make them inactive while in the selection screens.