- Home /
How to associate prefab with a variable in C#?
I'm following this tutorial to make a simple FPS except I do the code in C#. It says to instantiate a missile (rigid body sphere) I need to associate the Missile prefab with projectile variable and when the code is in JS it can be done by dragging and dropping since I see all the information about the script in the Inspector section. When it's C# Inspector only shows that my script is attached to the Launcher, no variables and all. How do I associate Missile prefab with projectile variable?
Just in case, here's my code:
using UnityEngine; using System.Collections;
public class MissileLauncher : MonoBehaviour {
Rigidbody projectile; int speed= 20; void Update (){ if(Input.GetButtonDown("Fire1")) { Rigidbody instantiatedProjectile=(Rigidbody)Instantiate (projectile,transform.position,transform.rotation); instantiatedProjectile.velocity=transform.TransformDirection(new Vector3(0,0,speed)); Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider); } } }
Answer by AeonIxion · Oct 26, 2012 at 09:38 AM
Make it a public variable.
C#-scripts only show public variables in the inspector. If you don't put public/private in front of a variable, it is private by default.
Your answer
Follow this Question
Related Questions
Only instantiate if clear space beside prefab 1 Answer
How to create new tail in a simple 3D Snake 0 Answers
How do i Instantiate a prefab with specific assests included 3 Answers
Two exact objects behaving differently... *scratches head* 0 Answers
Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers