I’m making a 2d game and I’ve created a script to shoot the arrows and another one to destroy it when it touches the limit.
Here is the script to shoot the arrow:
#pragma strict
var ACount : int = 40;
var arrow : Rigidbody; // Prefab of the arrow.
var projSpeed : float = 20f; // The speed of the projectile
var Rotation : Vector3; // Vector 3 to hold rotation
private var playerMove : CharacterController;// Reference to the PlayerControl script.
var fireRate : float;
private var nextFire : float;
function Update ()
{
// Check to see if Fire 1 is pressed
if(Input.GetButtonDown("Fire1") && Time.time > nextFire && ACount >= 1)
{
if(transform.localScale.x < 0)
{
ACount -= 1;
nextFire = Time.time + fireRate;
// Instantiate an arrow!
arrow = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
arrow.velocity = new Vector2(projSpeed, 0);
}
else
{
ACount -= 1;
nextFire = Time.time + fireRate;
// Instantiate an arrow!
arrow = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0, 9, 0)));
arrow.velocity = new Vector2(-projSpeed, 0);
}
}
}
And here is the script to destroy the arrow:
#pragma strict
var arrow : GameObject;
function OnTriggerEnter(other : Collider)
{
if (other.CompareTag ("Arrow"))
Destroy(other.gameObject);
}
The problem is when the arrow hits the limit not only does it destroy the arrow, it also erases it from the inspector in the scrip, making it impossible to fire more than one arrow, because theres not a rigidbody attached.
arrow = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0,9,0)));
you’re replacing the prefab with an instance because you are using the same variable name “arrow”.
myArrow = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0,9,0)));
this creates an instance of the prefab, and if you then use “myArrow” to set it’s variables you’ll be interacting with the instance.
I tried doing that but it showed me the following error:
Assets/Standard Assets/Character Controllers/Sources/Scripts/Shot.js(33,25): BCE0005: Unknown identifier: ‘myarrow’.
Also my code is in Java and the one you suggested is in C#. won’t that make a difference?
you would need to declare the new variable “myarrow” (Unity - Scripting API: Object.Instantiate)… and the code I posted was your’s copied with a “my” tacked on the front… would appear the code tags default to display c#… new forums fun fun fun
1 Like
Ok it’s working, thanks (:
But now I’m having another problem.
When my player dies I want him to start with 20 arrows. The idea I had was to use getcomponent to get var ACount from the shot script and put 20 instead of 40, but the script associated with the player dying is in a diferent gameobject so I dont know how to get the info.
Here is the script associated with the player dying:
#pragma strict
var Limite : GameObject;
var Player : GameObject;
var spawnPoint : Transform;
var count;
function OnTriggerEnter (other : Collider)
{
if (other.CompareTag ("Player"))
{
Limite.audio.Play();
Destroy (other.gameObject);
var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
// var AC = GameObject.FindWithTag("Graphics");
// AC.GetComponent(Shot).ACount = 20;
var count = GameObject.FindWithTag("Graphics").GetComponent(Shot);
count.ACount = 20;
}
else if (other.CompareTag ("Arrow"))
{
transform.position.z = 9;
Destroy (other.gameObject);
var AT = gameObject.GetComponent(AnimateTexture);
AT.rowNumber = 0;
AT.fps = 5;
var EW = gameObject.GetComponent(Enemywalk);
EW.step = 0;
Destroy(this.gameObject, 2);
}
}
Thanks again for the help so far.