Reputation: 41
I'm following a Udemy 2D tutorial in Unity to create a 2D pixel game. Language is C#. In my player script I have an attack function, such as when i press enter , a sword comes from the player body and go in which direction the player is looking ( like in the old zelda games ). In order to make the player freeze a little moment when he attacks, but also to make the sword range limited, i'm using a second script called Sword, the problem is that i think the script isn't called at all ( i tried to put some debug even in void start, but nothing.
here are the codes:
-This is my attack code:
void attack()
{
canMove = false;
GameObject newSword = Instantiate(sword, transform.position, sword.transform.rotation);
#region //SwordRotation
int swordDir = anim.GetInteger("dir");
if (swordDir == 0)
{
newSword.transform.Rotate(0, 0, 0);
newSword.GetComponent<Rigidbody2D>().AddForce(Vector2.up * thrustPower);
}
else if (swordDir == 1)
{
newSword.transform.Rotate(0, 0, 180);
newSword.GetComponent<Rigidbody2D>().AddForce(Vector2.down * thrustPower);
}
else if (swordDir == 2)
{
newSword.transform.Rotate(0, 0, 90);
newSword.GetComponent<Rigidbody2D>().AddForce(Vector2.left * thrustPower);
}
else if (swordDir == 3)
{
newSword.transform.Rotate(0, 0, -90);
newSword.GetComponent<Rigidbody2D>().AddForce(Vector2.right * thrustPower);
}
#endregion
}
and this is my Sword code : I used it primarly because when the attack is made canMove is set to false, so I have to reset it to true after a short amount time to be able to move again.
PS: what i noticed is that the sword script has been directly created in the unity interface in the scripts folder and is at first glance not associated to any gameobject.
{
public float timer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().canMove = true;
Destroy(gameObject);
}
}
}
Upvotes: 1
Views: 428
Reputation: 1
Does your prefab have the script attached at the point of instantiation? If not, you would still need to AddComponent< SwordScript>() on it. Make sure it's on a parent that holds the important stuff, like colliders and meshes, and not some smaller child, so that all of it gets destroyed.
Also looks like you're not giving your timer a value (float timer; intead of float timer = 10f;). So when you instantiate your sword it would immediately be destroyed. Maybe that's why you can't see it?
Off topic - I would advise against doing this in Update():
GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().canMove = true;
And instead consider giving the player a singleton, or at least getting that reference once at Start(). You're searching all gameobjects with that tag, trying to find the player for every sword in game 60 times per second, and getcomponent too. Idk if you have a good reason for doing this, but if not, consider changing that. Tbh not a huge deal if you're just experimenting and having fun, but still maybe something to consider at some point.
Upvotes: 0