Multiple sprites for a prefab in Unity - c#

Im making a japanese educational game and made this sprite sheet. Ive already sliced it in the sprite editor and named all the symbols.
Im instantiating 5 prefabs that need to have 5 different sprites with different
symbols. I thought I was gonna be able to either assign the entire sheet to a game object or just keep adding sprite renderers to the game object, but I was wrong.
How can I achieve this?

You can change the sprite directly from code.
this.GetComponent<SpriteRenderer>().sprite = someSprite;
You can dependency inject the sprites themselves using the editor and a public variable for each; or if you prefer, you can use Resources.Load instead.

Related

How to place a 3D object above the other one using Unity in AR?

I am working on an AR-based project in Unity using Vuforia.
Is there a way to place a 3D object above another 3D object?
For e.g. I have placed a table above which I have to place something else like a flower vase or a cube over an already spawned cube. How to achieve this?
Because as far as I know, Unity is detecting only floor surface and if I try to place the flower vase over the already kept table, it is placing it under the table overlapping the existing 3D object.
I believe that the PerformHitTest in the PlaceFinderBehaviour script needs to be modified to return the object surface during a Hittest. How do I do approach this? the script is not having modify access in Unity.

A question regarding Initializing gameobject in Unity

I'm learning unity3d and c# right now with c++ and c knowledge. However, I'm confused with this line of code.
private Gameobject cubePrelab;
This is a simple line of code which "defines the object that we want to spawn" according to the lecturer.
Then he immediately do things like:
Instantiate(cubePreLab, ...,...)
which clones the object according to the documentation online.
I'm confused about the first line of code which it seems like we are just defining an object without actually initializing it. However, the result is with these two line of code, we can actually create a box on the screen but I'm confused why it is a box instead of maybe a trangle.
I suggest you read about Unity Prefabs
Since you're new to Unity, I assume you've started off by putting a few objects -- such as a cube -- into your scene right? The Unity Editor makes it easy to put objects into your scene while you're designing your game, but what if you wanted to dynamically add objects into your scene while you're playing your game?
For example, imagine that you created a scene that has a cannon in it, and every time the player hits the Space key, you want to fire a cannonball. When you create your Scene in the Unity editor, you put your cannon in your scene, but how do you add cannonballs while you're playing the game?
You could have a bunch of code that creates a cannonball from scratch every time you fire one, but that's a very time consuming and error-prone way of doing it. This is where prefabs come in. A prefab is like a blueprint for an object that you're going to add to your scene later. So a cannonball prefab would be an object that you design in the Unity Editor just like any other Unity object that you're adding to your scene. You could assign its size, its material, its color, its attack power, and so on through the Unity Editor (no code needed).
First you would create a prefab in the Unity Editor for your cannonball.
After you create a cannonball prefab, you would add a property on your Cannon class such as public GameObject cannonballPrefab, and you'd drag your cannonball prefab from the Unity editor into the "Cannonball Prefab" property of the Cannonball component on your cannon object in the Unity Inspector. So if you only look at the C# code, it looks like the cannonballPrefab field is never getting initialized. However, you are initializing it by dragging a prefab into it in the Unity Editor. To reference your initial question, this is also how Unity knows to create a box and not a triangle in your example. Unity will create an instance of whatever your prefab is.
Then within your Cannon code, you can instantiate an instance of your cannonballPrefab while you're playing the game each time you want to add a cannonball into your scene.
Now when you call Instantiate(cannonballPrefab... Unity adds an instance of your cannonball into your scene. Then you'd probably do some additional initialization by assigning it an initial position, an initial speed, an initial direction, and so on.
Also, like Pac0 said, the tutorial probably has public GameObject cubePrefab (not private) so that you can drag a reference into it from the Unity Editor. Alternatively you could keep it private and add the [SerializeField] attribute to it like below. Unity won't let you assign a private field from the editor unless it has the [SerializeField] attribute on it.
[SerializeField]
private GameObject cubePrefab;

Loading all sub-sprites of a sprite

I'm trying to load all the sprites given the current sprite in the sprite renderer on an object however I am unable to load each individual sprite (2d sprite with multiple 'frames', i.e. a spritesheet) no matter what I try.
I've used
Resources.LoadAll<Sprite>(AssetDatabase.GetAssetPath(spriteRenderer.sprite.GetInstanceID()).Replace(".png", ""));
And even
Resources.LoadAll<Sprite>(Path);
But I can't seem to get it to load the things I want (usually it doesn't work at all but on an older version of Unity it loaded circular loading bars and error signs). I have created the frames/sub-sprites of the sprite.
I've tried changing the path to just using the sprite's name to including the filetype to anything else but nothing works.
I am using Unity 2019.1.0f2 if that matters.
So you want to load a bunch of sprites right?
Have you tried this?
Sprite[] loadedSprites = Resources.LoadAll("Sprites", typeof(Sprite));
If you use this your unity must have a folder named Resources. Basically what it says inside LoadAll at the first param is this: "Resources/Sprites" and then it looks for all Types that are a Sprite inside the Sprite folder.
Note: Make sure all the .png files that are planned to be used are signed as Sprite(2D and UI) this can be done by clicking of the png in unity and right corner it wil say Texture Type.
It ended up being a simple misunderstanding.
My folders were all inside the "Assets" folder while they should have been in the "Assets/Resources" folder. Now I can load the sprite renderer's sprite's sprites using
spriteRenderer.sprite.GetInstanceID()).Replace(".png","").Replace("Assets/Resources/","")
It's a bit long but it works!
Thanks to Louis Ingenthron for the hint!

Unity3D-how to change scale of avatar

I downloaded a 3D model from the Asset Store. I changed the "Scale Factor" of the model to 0.25
I'm using this model in 2 different scenes and they need to be different sizes, one Scaled:0.25 and the other Scaled:0.40
How can I have the model in the other scene be scaled to 0.40?
Adjusting the scale of the transform of the model in the scene doesn't change the size of the model.
Please help. Thank you!
UPDATE: I'm learning more as I'm trying to figure this out. The original model is composed of Skinned Mesh Renderers (different costumes).
I decided to try something random, I made a 3D object (a sphere) which I could adjust the scale without issue. I then selected the mesh of the model for the MESH FILTER, and then I selected the material for the MESH RENDERER. I now have the model that I could scale without issue. Now the problem is, I made the gameObject a child of an empty gameObject and added an animator component to the parent gameObject, but it doesn't run any animations.
Made it a child of an empty gameObject with an animator component, but doesn't run any animations
Please advise.
Set correct basic scale in imported model:
http://i.stack.imgur.com/i7G1l.jpg
Create 2 objects on the scene.
Set scale of every single object to needed inside of "transform" component
Create prefabs Elf0.4 and Elf0.25 from created objects
delete from scene those objects.
use those prefabs on the needed scenes.

Unity 2D Instaniate prefab with text

I'm new to Unity3D and looking for some basic information.
I'm used to OO programming, but I cannot quite see how to access the objects from script.
I created an object, made it a prefab (plan on using it many times) and the object has text on it.
The text uses a Text Mesh. I'm using C#.
How do I thru code, simple example on start, instantiate a new prefab object called Tile at 0,0?
How do you access the text Mesh part of the object to change the text?
I sure there is something simple I'm not picking up on.
Just having trouble understanding how to connect the code to the objects and vice versa.
Updated:
Also wanted to note, I'm trying to load multiple objects at the start, if that makes a difference in the answers.
Update2:
Just wanted to explain a little more what info I was missing when tying the mono code to the unity interface.
In Unity:
Created any object and turn it into a prefab.
Created a second empty game object, place it somewhere in the play view area.
Created a script on the Empty game object.
In Mono code editor:
Created 2 public variables (C#)
public GameObject spawnObj;
public GameObject spawnPoint;
void Update () {
Instantiate (this.spawnObj, this.spawnPoint.transform.position, this.spawnPoint.transform.rotation);
}
Back in Unity:
Select the Empty Game Object.
In the script Component, you should see the 2 vars.
Drag your Prefab Object into the var spawnObj.
Drag the Empty game object into the var spawnPoint.
I did this is the Update, not to smart, but it spawned a cube or 2 or more, spawning from code is all I wanted to understand.
AD1: It's GameObject.Instantiate:
var go=GameObject.Instantiate(prefab, Vector3.zero, Quaternion.Identity) as GameObject;
A prefab is an asset-like GameObject that is used as a template for scene GameObjects.
To use a prefab you have to drag a GameObject to the project window and reference it in code in one of two ways:
Using resources, var prefab=Resources.Load("my-prefab") will load the project file Resources/my-prefab. Note the use of the special "Resources" directory - it's a magic name that is required.
Using a reference, in a MonoBehaviour add a public GameObject prefab field. Then on a GameObject using this class you can drag and drop your prefab from the project window to create a reference to it. You can then use the prefab field in your code. No "Resources" directory needed here.
Prefer option 2, as all resources are in the final binary, while normal assets are trimmed when possible.
AD2: get the TextMesh compontent and modify text:
go.GetComponent<TextMesh>().text="new text";

Categories