Unable to add UI elements in hierarchy to prefab - unity3d

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>();
}

Related

How to avoid this Unity Prefab Modifications behavior?

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.

Instantaited Rigidbody not changing bodytype

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.

How do I replace a sprite in unity with another one?

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.

Unity: Is there a way to get all GameObjects/Components that were used in a Prefab?

A bit new to Unity, I know that you can get a prefab from the instance of the GameObject (right click on GameObject and Select Prefab). But is there a way to select the GameObjects and Components that were used to created a Prefab? (from the Prefab)
Try this example out. It contains two flavors of selection, one to select all components attached to the prefab, and the other to select just the monobehaviours attached to the prefab.
What this script will do is check what components / monobehaviours are attached to the currently selected GameObject (or prefab) and will output them to the log, as well as select them all.
Copy the below code, put it into a new script named "PrefabComponentSelectorEditor" and save it in a folder called "Editor".
You should then see a Menu Item called "AxS -> Prefab Component Selector". Click on it, and see the console as well as the inspector.
The code is commented, but in case you have any questions, feel free to ask.
P.S. Remember to use ONLY ONE of the flavours (i.e. Monobehvaiour or Component). Comment / Uncomment the code accordingly. I have left the component version uncommented, and the monobehaviour version is right below it.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class PrefabComponentSelectorEditor : EditorWindow {
[MenuItem("AxS/Prefab Component Selector")]
public static void SelectComponents () {
Debug.Log ("GOs "+Selection.activeGameObject);
if(Selection.activeGameObject != null) {
//This will select all the COMPONENTS attached to the prefab
//Let's first get all the components this GameObject has
Component[] components = Selection.activeGameObject.GetComponents<Component>() as Component[];
//If the length of components is > 0 (always WILL be, since every GameObject is
//guarenteed to have a Transform Component at the very minimum
if(components.Length > 0) {
//Print all the components to console
foreach(Component component in components)
Debug.Log (component);
//Set the current selection to the components
Selection.objects = (Object[])components;
}
//This will select all the MONOBEHAVIOURS attached to the prefab
//First get all Monobehavious attached
//MonoBehaviour[] behaviours = Selection.activeGameObject.GetComponents<MonoBehaviour>() as MonoBehaviour[];
//Check if the length of the array is > 0. This is required, since a GameObject
//may have no scripts attached to it
//if(behaviours.Length > 0) {
//Print all attached Monobehvaiours to console
// foreach(MonoBehaviour behaviour in behaviours)
// Debug.Log (behaviour);
//Set the current selection to the Monobehaviours
// Selection.objects = (Object[])behaviours;
//}
//The difference between COMPONENT and MONOBEHAVIOUR
//A Monobehaviour is basically any script you make, so if you use the monobehaviour version
//it will only select the scripts you attach to the prefab
//A component is anything you can see on the inspector, including Unity's inbuilt components
//Essentially, all Monobehaviours are Components
//Eg. If your prefab has a single script called "ExampleScript" attached to it, and has a BoxCollider
//Monobehaviour version will output just the ExampleScript
//Component version will output the Transform, BoxCollider and ExampleScript
}
}
}

how to build AssetBundle from Gameobject in hierarchy not selection?

i am trying to automate the process of manual selection of game object from hierarchy then drag into the project panel by mouse then select Track dependencies from the menu and exporting the prefab into .Unity3D file
i want to automate this by code .
i think of the process like ... Create prefab then buildpipeline.BuildAssetBundle ,,,, but the first 2 parameters in buildpipeline.BuildAssetBundle are the Problem i can't make them fit with a gameobject nor prefab
the first 1 takes (object ) as main asset and the second takes (object[]) which refer to the inside asset files ..... but i can't put this to refer to a gameobject nor prefab .
GameObject maw;
string paths="Assets";
public PrefabType dude;
void Start () {
dude=PrefabUtility.CreatePrefab("Assets\temp",maw);
// the problem is here the first 2 parameters ......
BuildPipeline.BuildAssetBundle((Object) dude, (Object[])dude, "Assets",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets , BuildTarget.Android);
}