Unity: Initializing smoke Prefab via script - c#

I am trying to use a smoke prefab (free asset from Unity asset store). When I add the prefab directly into the game scene, the prefab works:
But when I create the following script to initialize the same prefab (inside a game object), nothing happens:
class Smoke1 : MonoBehaviour
{
public GameObject myPrefab;
void Start()
{
Instantiate(myPrefab, transform.position, transform.rotation);
}
void Update()
{
}
}
My game scene:
Can you help?

Try to use Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);. This will spawn the prefab in the center of the scene. If it works, then it means you're doing something weird with the position and rotation of the object upon instantiation.

Related

How can i clone an object one by one in unity

So basically i wanted to learn about instantiate and I have tried this code below:
public Transform spawnpos;// the position where i wanted to clone my sphere
void Update()
{
if (Input.GetKeyDown("q"))
{
Instantiate(gameObject, spawnpos.position, spawnpos.rotation);
Destroy(gameObject, 2);
}
}
the thing is, when i press "q" it does spawn a sphere and gone in about 2 secs, but if i continously press "q" the amount of clones spawned are getting bigger as i press and it wont be destroyed after 2 seconds.
In Conclusion, how can i spawn a sphere when i pressed "q", and it will spawn 10 if i pressed "q" 10 times
tysm for reading this far! have a nice day! :)
First thing's first, you can't instantiate the gameobject to which this script is attached through this script because:
If you instantiate a gameobject, there will be two gameobjects in the scene and both will be having this script. Hence if you press Q again, two gameobjects are instantiated.
Solution:
Create an empty gameobject in the scene and create a new script ObjectSpawner.cs and attach it to this empty gameobject.
public class ObjectSpawner : MonoBehaviour {
public Transform spawnpos;// the position where I wanted to clone my sphere
public GameObject sphere;// gameobject you want to spawn
void Update() {
if (Input.GetKeyDown("q")) {
GameObject obj = Instantiate(sphere, spawnpos.position, spawnpos.rotation);
Destroy(obj, 2);
}
}
}
In the above script as you can see there's a field for the sphere gameobject. Drag it to the slot in the inspector.

Spawning Bullets

So i am really, really new to Unity and C# and i just want to try a few things like a shooting script. The way i want it to work is that i have a game object, in my case it's called "Glock" and as a child of that game object i have an empty called "bulletSpawn". I now want to assign a script to "bulletSpawn" that spawns in an predefined object whenever i hit the left mouse button.
What i tried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnBullet : MonoBehaviour
{
public GameObject bullet;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
GameObject newBullet = Instantiate(bullet, new Vector3(0, 0, 0), Quaternion.identity);
}
}
}
And it kinda works. Whatever, it clones everything in an exponential way, so when i hit the left mouse button it spawns in the object but it also spawns in an object called "Glock(Clone)" and if i hit it again, it spawns: "Glock(Clone)","Glock(Clone)(Clone)","bullet(Clone)" and "bullet(Clone)(Clone)". And that doubles each time i click. My second problem is, that it spawns every object at the global coordinates [0,0,0] and not the local ones of the empty.
As Helmi say are you using a bullet prefab and not the Glock prefab?
This code under should fix your position problem.
if (Input.GetButtonDown("Fire1"))
{
GameObject newBullet = Instantiate(bullet, transform.position, Quaternion.identity);
}

Unity3D Help - Shooting object in direction of player rotation

I'm making a third person sports game and I wish to shoot the ball straight forward from the player.
I have a working script for shooting the ball, however it is attached to the main camera so only shoots in the direction the camera is facing.
I would like to alter this code to attach it to the player instead of the camera.
PS: I am still very new to C#, I assume the problem is in the "camera.main.transform" section but I don't know the code to change it to player.
my code is below;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootScript : MonoBehaviour
{
public GameObject bulletPrefab;
public float shootSpeed = 300;
Transform cameraTransform;
void Start()
{
cameraTransform = camera.main.transform;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
shootBullet();
}
}
void shootBullet()
{
//Get the Rigidbody that is attached to that instantiated bullet
Rigidbody projectile = GetComponent<Rigidbody>();
//Shoot the Bullet
projectile.velocity = cameraTransform.forward * shootSpeed;
}
}
First of all: You currently do GetComponent<Rigidbody> on that object itself .. this makes no sense since it would fire away "yourself" instead of a bullet. You have to Instantiate a bullet and apply forces/velocity here. You kind of missed the instantiation.
Then simply attach this script to your player Object instead and use transform.forward. transform returns the Transform component of the GameObject this script is attached to.
public class ShootScript : MonoBehaviour
{
// Hint: By making the prefab field Rigidbody you later can skip GetComponent
public Rigidbody bulletPrefab;
public float shootSpeed = 300;
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
shootBullet();
}
}
void shootBullet()
{
// Instantiate a new bullet at the players position and rotation
// later you might want to add an offset here or
// use a dedicated spawn transform under the player
var projectile = Instantiate (bulletPrefab, transform.position, transform.rotation);
//Shoot the Bullet in the forward direction of the player
projectile.velocity = transform.forward * shootSpeed;
}
}

Play/Stop animation/animator on instantiated object

I'm developing a unity game and basically, I have a prefab with a sprite inside it. I created an animation attached to that sprite.
FrogPrefab
|__ FrogSprite
I created a script with a public field "prefab" where I pass my prefab.
My question is, how can I stop and play this animation from my script.
I instantiated my prefab from my start method...
public GameObject gameCharacterPrefab;
private GameObject frog;
void start() {
frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
}
I'm trying to do something like that...
frog.animation.stop();
appreciate any help
First, note that the function should be called Start not start. Maybe this is a typo in the question but it's worth mentioning.
Use GetComponent to get the Animator or Animation component. If the animation is a child of the prefab then use GetComponentInChildren.
If using the Animator component:
public GameObject gameCharacterPrefab;
private GameObject frog;
Vector3 objectPoolPosition = Vector3.zero;
Animator anim;
Instantiate the prefab
frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
Get the Animator component
anim = frog.GetComponent<Animator>();
Play animation state
anim.Play("AnimStateName");
Stop animation
anim.StopPlayback();
If using the Animation component:
public GameObject gameCharacterPrefab;
private GameObject frog;
Vector3 objectPoolPosition = Vector3.zero;
Animation anim;
Instantiate the prefab
frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
Get the Animation component
anim = frog.GetComponent<Animation>();
Play animation name
anim.Play("AnimName");
Stop animation
anim.Stop();
For playing animation we can use Play() but for stopping the animation the Stop method is become obsolete in Unity 2019 or higher versions. So, For disable animation we can use enable flag and set it to false.
//For playing the animation
frog.GetComponent<Animator>().Play();
or
frog.GetComponent<Animator>().enable = true;
//For stop the animation
frog.GetComponent<Animator>().enabled = false;

How to instantiate nonpublic prefab

I instantiate prefabs by dragging them into a variable in the scene. Here's the code:
public GameObject player1;
void Start()
{
Instantiate(player1, spawn.position, spawn.rotation)
}
I don't want to drag and drop. How do I accomplish this?
Put your prefab in the Assets/Resources folder.
Then you can find the prefab and then instantiate it.
private GameObject player1;
void Start()
{
player1 = Instantiate(Resources.Load("Player1"), spawn.position, spawn.rotation) as GameObject;
}
"Player1" is the name of the prefab. You can call it what you want.
You can do this with any prefab or other data, such as textures. More information on Resources.Load on Unity API.

Categories

Resources