Hello! I am new to the C# language and I’m having some issues with Instantiating a prefab in Unity. I’m making a Tanks-esque game, and I wish to spawn my prefab (a model with a rigidbody) at the turret of my tank. I had it working, but was having rotation issues. So, I attempted fixing the model, then re-importing. I also cleaned up what I thought was unnecessary code, but it turned out that it was important. I couldn’t find any help by googling, so I’m looking for solutions.
You cannot Find(…) prefab…
Why not write
public GameObject Projectile_RED;
near your RigidBody line and drag prefab onto that script in inspector?
I’m not quite sure what you mean by dragging the pre-fab onto the script in inspector. I know that Unity can’t find my pre-fab (which is “Projectile_RED”).
Because you use Find(…). It won’t return you anything except for active existing GameObject that is on scene.
I’m telling you to first create prefab by dragging GameObject into project panel (where all files are listed) and then dragging that prefab onto variable you’d create by adding the line I’ve written to script.
Or you can create a GameObject with name “Bullet.prefab” somewhere on scene, but it needs to be active for that code to work. But that means you’ll have some bullet flying somewhere that shouldn’t exist.
I’ve found in recent versions of Unity that loading a scene additively will cause Resources.Load() to always return null. It’s not a pretty solution, but to fix it, I put deactivated copies of my prefabs in the scene, and I duplicate and activate one when I need it.
EDIT: I’ve also found that references to a prefab in the resources folder become null when you load scenes additively too.
Hello again! Sorry for the delay. I added the prefab to the variable in the script, like you said. Now, I’m getting an error that says that the variable is not being used.
It sais “You didn’t assign it”. Click on gameobject containing that script in hierarcy in editor and drag’n’drop prefab onto field of that projectile in inspector.
Sweet. That made it spawn. Unfortunately, it’s not transforming itself to fit the position and rotation of Cube_004, which is the turret of the Tank.
Scratch that, it is, it’s just not applying the pre-fabs constant force. And it’s giving the aforementioned error.
Error is because for some reason it can’t Find(“RED_Cube_004”). I don’t know why and that’s one of reasons Find(…) is generally bad. You can just remove variable and all lines that use it because projectile position and rotation are being set from parent of your Bullet_Launch_Red script already in Instantiate call.
Prefabs don’t have any force or velocity stored. You need to set velocity using Projectile.velocity= some vector3 where you shoot after your Instantiate call. (You could add force, but why?).
Ah okay. What would something like that look like? Would I need to set up a new variable (Obviously I’ll want to, so I can easily adjust the bullets velocity for both tanks at once.)? Maybe an example of code would be helpful. I get what you mean, but not sure how to write it.
Depends on what you want. For example:
Rigidbody Projectile = Instantiate(Projectile_RED, transform.position, transform.rotation) as Rigidbody;//That's line you already have
if(Projectile!=null)//Does it has Rigidbody component?
{
Projectile.velocity = new Vector3(1,0,0);//Always shoot by X axis
}
or
//Before Update() method
public GameObject target;//Set it in inspector to object it should be firing at
public float projectileSpeed = 1;//Speed of projectile, can modify in inspector
//Inside Update() method:
Rigidbody Projectile = Instantiate(Projectile_RED, transform.position, transform.rotation) as Rigidbody;//That's line you already have
if(Projectile!=null && target!=null)//Does it has Rigidbody component and target is set?
{
Projectile.velocity = (target.transform.position - transform.position).normalized * projectileSpeed;//Shoot in direction of target
}
or you could raycast from camera to position where mouse points and shoot there (there’s plenty of manuals/tutorials/answers/topics with that).
Alright. I’ll get back to you on Tuesday on whether or not this all works. (I’m currently in high school and making a game for an independent study). Thank you so much for all the help and patience.
I’ll say it again in case you missed it… in some recent versions of Unity, everything in your resources folder will go null when you additively load a scene.
Alright, so now I’m error free! When I press “r”, the projectile spawns at the base of the cannon, oriented with the tank. Unfortunately, it won’t move. I also need to rotate it 90 degrees, so it is pointing the right way. How would I go about doing that? I’m assuming I could do something along the lines of “Projectile_Red.transform.rotation = (0,0,90)” but not sure if that’s correct or where to put it. The movement is sort of just confusing me. Do I need a property applied to the prefab? Or do I need to create a float variable?
Alright, my problem has been solved! Thank you so much for the help. My tanks are both properly spawning their bullets and firing them how I want.