My Prefab disappears from script when I run the game? - unity3d

I have Script attached on my Player, that script has Public GameObject variable.When I attach my Prefab to it and run game it disappears from script ?
I add my prefab to script
When I run the game it just disappears !!??

the bomb prefab that you attach must be in under your asset folder , not in the scene , I mean when you click on the bomb that you assigned it must open project tab and shows it to you if it only exist in scene , it will be disappeared

It could be that the bomb prefab you are assigning to your public GameObject variable is somehow being destroyed when you start the game.
For example:
public GameObject other;
private void Awake()
{
Destroy(other);
}
Check to see if you have any Destroy functions that could be causing the prefab to be destroyed.

Related

Attaching prefab's script to another prefab as a component

I have Player prefab with script Player
Also I have GameController prefab with script TouchMove.
This is TouchMove code:
public class TouchMove{
[SerializeField] Player player;
.....
}
So TouchMove has player object as a component, which I attach through unity inspector.
TouchMove script is responsible for player movement. That is why it neads the player object.
Also I have a lot of scenes and each one contains GameController and Player objects.
When I modify the GameController or Player components I wanted the all game objects at the all scenes to be updated. That is why I decided to create prefabs: GameController prefab and Player prefab.
In inspector I added Player prefab to GameController prefab because TouchMove of GameController requares player object.
But after it when I run the game the player script gets not active. I mean that I get unable to move player in the game. (the player script gets not working)
This problem solves if I attach Player to GameController as just game objects (not the prefabs or without prefabs)
So, my questions:
why player scripts gets not working if I attach it with prefabs and it works if I attach just as game objects (without prefabs)
Is it possible to link two prefabs in the way I described?
The reason you are getting this problem is that player prefab and specific instance of the player prefab - are not the same object. They are both GameObject-s, and can both be referenced.
In your case GameController references a player prefab, but the player in the scene is a new object, which was created on scene start by CLONING the player prefab. Since it was created only on scene start - another prefab can never reference it.
To solve this, you can take couple approaches.
First, parent both player and game controller under 1 new object, call it GameManager for example. Then you will have only one prefab - game manager, that contains both player and controller.
Second option, the one I usually use, is to find the player object at load time. Instead of passing player s a public argument, find it on awake, for example like this:
private Player player;
void Awake() {
player = FindObjectOfType<Player>();
Assert.IsNotNull(player);
}
Prefabs need to exist (be instantiated) in scenes to function, what you're attempting to do is a workflow of scriptable objects.
Instead of attaching a source prefab from your project directories through the inspector, try looking for an existing instance of the prefab in the scene (at runtime).
private void Awake()
{
player = FindObjectOfType<Player>();
}

Unity, keep playing music on different scenes

So I've been looking around and no solution seems to work to me...
I want to play some background music in all the scenes without resetting the audio.
I've a prefab with an audio source and a scritp.
the script does:
private void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("BGAudio");
if (objs.Length > 1)
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
}
So I also tagged the prefab with BGAudio but when i change the scene it stops the music. If i add the prefab to both scenes it starts from 0..
I tried also doing a singleton but that doesn't work either.
I'm using unity 2019
I'm using an android build which i doesn't think it changes anything but just in case.
Just found out that DontDestroyOnLoad only works for objects on the root. While my Bgsound was inside a bg empty object.
You can also create a brand new scene that only includes your BGAudio object. Load both your BGAudio scene and play scene as additive.
For example: create a BGSound scene. Then create an empty gameobject called BGSoundManager. Also create your BGSound object. Then add a BGSoundManager script to your BGSoundManager gameobject and edit it like that :
private IEnumerator Start()
{
yield return SceneManager.LoadSceneAsync("BGSound", LoadSceneMode.Additive);
yield return SceneManager.LoadSceneAsync("Level1", LoadSceneMode.Additive);
Destroy(gameObject);
}

How to add a Tilemap to a prefab?

I have a Bomb prefab that gets dinamically added to the center of a 2D tile on keypress via a BombSpawner script. When the bomb explodes, I want it to affect multiple tiles in my Ground tilemap.
The explosion is inside the BombController script which is part of my Bomb prefab. I tried accessing the tilemap by adding it manually to the prefab:
public Tilemap tilemap;
But it doesn't work. I can only drag and drop the tilemap to the Bomb when it's in the scene, not to the prefab.
Is there a way to access the Ground tilemap inside the BombController script?
So, This is what is normal in unity when you are working with prefabs. You can only assign components/scripts anywhere in prefab if that component/script is a part of the prefab itself.
If component/script is in your scene hierarchy but not in the prefab. In your case, it was Tilemap object. which was in your scene hierarchy but was not the part of your prefab. Even if you edit the prefab directly in the scene view(Don't get confused with prefab editor which looks as same as scene view). and save the prefab the BombController.tilemap will always be null(None). Something Like
have you tried importing this library from unity
using UnityEngine.Tilemaps;
and then do that variable
public Tilemap tilemap;
enter image description here

SystemEvent Prefab's GameObjects not working

I have Button Prefab. It's OnClick attached to EventSystem Prefab and select method called ( doIt ).
This method (doIt) print message and enable panel called ( Panel).
public void doIt()
{
print("Doing !!!");
helpCanvas.SetActive(true);
}
EventSystem Prefab has script which has method ( doIt ) and gameObject ( Button ) already attached to it
Panel Prefab is inactive
I draged these Prefab ( Button, EventSystem and Panel ) in some scenes (about 17 scenes) like this.
The problem is:
When i play the game there is nothing happen when i click button
, but the panel become enabled just after i stop the game
Note:
This happened only when the SystemEvent instance depend on prefab , but if i draged the panel manually from the scene
and the SystemEvent instance to the OnClick this not happened
Why this happened ? and how to solve that ?
It doesn't suit me or to any one to reattach the Button OnClick EventSystem Manually for all buttons which has the same function. Or to attach Panel instance to each EventSystem Instance.
Prefabs cannot have references to objects within a scene. This is because every time Unity instantiates the prefab, how is it supposed to know if the object referenced is still there. The workaround is to add a script component on your button prefab, and then create a function inside that which preforms your desired actions. Your button prefab can have a reference only to things which are local to it (not scene dependent, will be there every time the prefab is instantiated). After you instantiate the object, your master script can do buttonInstantiatedPrefabName.GetComponent().evsys = evsys; This way, all your editor references will be stored in a master script, which will assign it to other gameObjects which are instantiated from prefabs.

playing particle system in Unity

I am using Unity3D to develop for the HTV Vive using SteamVR. I have downloaded an asset from the asset store with explosion effect created using a particle system. I want to play the particle animation when an object is destroyed. Here is the code I am, unsuccessfully, using.
private void OnDestroy() {
explosion.GetComponent<ParticleSystem>().Play();
}
Explosion is a public variable of type GameObject set from the inspector. I drop the particle system object there.
Why is it not working? anyone a good recommendation on a short tutorial to learn to use (not to create) particle effects?
Thanks
view of the hierarchy
I have tried this with the PS as a child of the target and as an independent object.
view of the inspector (Target)
view of the inspector (particle system)
edit: for some reason, the particle effect is destroyed right after the scene starts.
Try making the explosion effect into a prefab and instantiate it when destroyed.
GameObject explosion; // Prefab asset
private void OnDestroy() {
Instantiate(explosion, transform.position, Quaternion.identity);
}
Also, don't forget the stop action to Destroy.