Reloading a scene through a coroutine changes textures [duplicate] - c#

I completed Unity's roll-a-ball tutorial and it works fine. I changed a couple of materials to make it look better. I also added a C# script that should restart the level when the player falls off of the ground (I disables the walls). I am using Unity 5.5.
It initially looks like this:
But when I go off the edge and the level restarts, it looks like this:
It sometimes looks like this for a few seconds after opening unity when the editor is loading.
Here is the script:
using UnityEngine;
using System.Collections;
public class DeathTrigger : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag("Player"))
Application.LoadLevel(Application.loadedLevel);
}
}
Any ideas on what's causing this?

The colors and materials are loaded. This is a lighting problem because lighliting is still calculating in the background. This will likely occur in the Editor only. This should not happen in the build.
Depending on your Unity version, you can fix this by going to Windows --> Lighting --> Settings then go to the Scene tab. Scroll down and disable Auto Generate checkbox then click the Generate Lightning button.
For older version of Unity with no Auto Generate checkbox, see here.

After play with Lighting tools, only one thing should be change on Lighting Setting for every scene.
Window > Rendering > Lighting (Unity 2020)
Click at Environment Tab
At Environment Lighting, change Source from Skybox to Color.
Select white color from Ambient Color.
Done. Try test it.
Before
After

I found many solutions online but I was missing out a step, so I hope this helps.
Most solutions indicated to go to Windows->Lightning, then untick Auto and Click Generate Lighting. My problem was that while I was pressing the generate button I did not have all of my scenes loaded for preview (at least not the scene I had problems with), so it was only applying light generation to the loaded scenes. Make sure all scenes are loaded when generating the lights.

Try Clearing Baked Data if you are using unity version around 5.5
Go to Windows->Lightning->Untick Auto->Now Click dropdown arrow of Build Button which is near Auto(Check Box) -> Select Clear Baked Data.
Now try Your code which looks fine although SceneManager.LoadScene (1); is enough.
Also unloading the previous scene and setting new scene as active scene is a good practice.
This worked for me.

File > Build settings > player Settings > (on the left) Graphics > (Top-Right) gear icon > Reset

I'm a newbie and none of the advice on web helped me. However when I went to Window > Rendering > Lightning > scene tab; If "lightning setting" says “none”, click on it and choose “demo”- setting. Press “generate”.
So it seems like it was missing settings all together which made the scene go dark when loaded.

I encountered the exact same problem. What worked for me was to set Directional Light > Light > Mode to Realtime. (it was Baked, for some reason)
I hope this can help someone in the future.

Related

Unity game view too dark on runtime

I made a simple game menu in unity, but it's looking way too dark in the game view (and in runtime), I tried:
Window->Rendering->Lightning Settings and set the Environment Lightning's Source to color and Light Grey color, also checked the Auto Generate option in Lightmapping settings but nothing of the above worked, it still looks way too dark. Is there any fix for this?
Sorry for the small image in the first link but I think it's very noticeable the difference in the colors anyway.
In the scene tab, you have to check the little sun ☼ next to the "Shaded" drop down so as to see the real lightning of your scene.
Since you have no light in your scene (except the ambient one) your models look very dark. Add a directional light to your scene (GameObject > Light > Directional Light)

The animation state .. could not be played because it couldn't be found?

I am trying to start an animation by pressing a key on my keyboard.
For this I am using the following code:
[HideInInspector] public Animation animation;
private void Start()
{
animation = GetComponent<Animation>();
}
private void Update()
{
if (Input.GetKeyDown("1"))
{
animation.Play("AnimationName");
}
}
This gives me the following error:
The animation state AnimationName could not be played because it
couldn't be found!
Although, the animation DOES start, but how can I clear this error?
I think the only possible explaination is that your animation clip is not marked as Legacy.
https://www.unity3dtips.com/zh/the-animation-state-could-not-be-played-because-it-couldnt-be-found/
EDIT with step by step here:
Animations must be marked as legacy to be used with the animation component.
In the project window select the animation clips you’re trying to play.
Set the inspector to debug mode which exposes hidden variables, in this case it’ll make the “Legacy” checkbox appear.Unity Inspector Debug Mode
Tick the “Legacy” checkbox and change the inspector back to normal mode.
Unity Inspector Animation Clip Legacy Checkbox
But there is one thing you should know: you should avoid, if possible, the old legacy way to play animation.
Learn how to use an Animator component. Just create an animator, assign your animation clip, decide a boolean for start the animation in the animator component and set it to true in your code (for basic use).
Sure there is some things to do beside code but it will be easier to control your animation, bleand different animations and change them to your needs (it's for example much easier to loop or stop the animation at some point).
It would be also easier to understand when it start and when is playing.
Another thing to check is the legacy 'Animation' component on your gameObject that is supposed to perform the animation. In my case things were marked as legacy for the animation on the imported animation but somehow when adjusting settings on my import it dropped the animation it was complaining about from the actual 'Animation' component so once I added it things worked as expected
Inspector in Debug mode showed my animation marked as 'Legacy' true
My Animation component showed the missing animation

I can't assign scripts to buttons in UNITY?

I just started exploring Unity and I'm making a game in UNITY 2D.
I decided to start with a simple menu screen to get used to UNITY.
I made the menu and moved on to programming the buttons.
However when I drag the script (or the game object holding the script) in to the button on click section and try and select my function it does not show up.
My unity window
Manager in the button on click script.
My code in C# (this is my first time using c#)
what I want to know is: have I done something wrong; doe unity work differently to how I understand it; is unity doing something weird; how do I fix it.
(appologies for the weird post layout I don't know why it did that).
Just insert a ; at the end of your code and you should be fine. You should notice that the compiler is complaining about the missing semicolon. Also, make sure to attach your script to the _Manager GameObject.
You need to :
add a semi colon to the end of the line in the Buttons class
Add Buttons Class to the _Manager gameobject in the scene

Play animation once?

I've made a simple animation for my dishwashing machine in 3dsmax. I imported the object as an fbx file into Unity. The animation is called "openandclose". The animation itself opens and closes the machine.
However I can't seem to find a way to trigger the animation to happen once at the click of a button. How can I do this?
Unity has a great documentation regarding every core function including animations. Check this link out: http://docs.unity3d.com/462/Documentation/ScriptReference/Animation.Play.html
and to get the button pressed:
http://docs.unity3d.com/462/Documentation/ScriptReference/Input.GetKeyDown.html
combined it would be something like this i guess:
void Update() {
if (Input.GetKeyDown("e"))
animation.Play("openandclose");
}

Unity; animation on prefab

Tried to find this kind of problem around the net but failed so...
Here's the thing - I have a prefab gameobject that is to represent a unit, portrait more specifically. It has several scripts attached and an Animation component with two animations: Static and Selected.
The prefab is instantiated, moved freely and, after placing, it can be clicked to select it, which should, aside from executing a bit of code, start the Selected animation.
Using this code:
void OnMouseDown(){
//
//Some inside stuff
//
if (this.GetComponent<UnitHandling> ().thisUnit.Selected)
this.animation.Play("Selected");
if(this.animation.IsPlaying ("Selected"))
Debug.Log("Animation of selection is playing");
}
I checked that the animation seems to be playing (at least the Debug message is showing) but I see no animation... What am I doing wrong?
Try making an animation state using mechanim, and play it using this:
GetComponent<Animator>().CrossFade("Selected", 0);
https://docs.unity3d.com/Documentation/ScriptReference/Animator.CrossFade.html
https://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html

Categories

Resources