- Home /
Scriptable object not referencing speed but references damage...
So I have 2 variable of Speed and Damage in a scriptable object.
Here is the code for the first section :-
[CreateAssetMenu(fileName = "BulletData", menuName = "Data/Bullet")]
public class BulletDataScriptableObject : ScriptableObject
{
public float speed = 5f;
public int damage = 30;
}
I then have a prefab object referencing this to fire a 'FireBall' which is utilising the following code :-
> using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerProjectiles : MonoBehaviour
{
public Animator animator;
public BulletDataScriptableObject bulletData;
public float speed;
public int damage;
public Rigidbody2D rb;
public GameObject FireEmbers;
public float liveTime = 1f;
void Start()
{
rb.velocity = transform.right * speed;
speed = bulletData.speed;
damage = bulletData.damage;
}
void OnTriggerEnter2D(Collider2D hitInfo)
{
Enemy enemy = hitInfo.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
Destroy(gameObject);
Instantiate(FireEmbers, transform.position, Quaternion.identity);
}
void Update()
{
liveTime -= Time.deltaTime;
if (liveTime <= 0)
{
Destroy(this.gameObject);
Instantiate(FireEmbers, transform.position, Quaternion.identity);
}
}
}
Here's the problem. For some reason my fireball will reference the damage variable but not the speed so when I fire the fireball it doesn't go anywhere unless I manually enter the speed variable in the Unity inspector, damage however it references fine and if I stand point blank at an enemy the fireball will damage them at the right amount. My item power-ups also damage them at the amended power level so I am confused as to why it's not referencing the speed variable.
@Aviryx If you can help again then I officially owe you a beer or something!
I think its referencing the speed variable but its not adding it to your rigidbody. In the start method, its setting rb.velocity
to transform.right speed; when it should be transform.right * speed; Let me know if this was a typo or an actual syntax error
It actually is transform.right * speed, the format of the post was screwed up so I've amended it so it should be more legible now. I'm hoping you can find another issue because it's baffling the crap out of me!
rb.velocity = transform.right * speed;
speed = bulletData.speed;
Are you positive that you want to assign rb.velocity with speed before you setup speed variable with bulletData.speed?
Answer by unity_McDQyp7qhCTdXw · Aug 26, 2020 at 09:42 AM
It was indeed the order of the code. Thanks everyone! :)
Your answer
Follow this Question
Related Questions
Tilemaps - Create Multiple Levels 0 Answers
2D GAME (non UI object) for every pc resolution 0 Answers
How can I instantiate a gameobject facing another gameobject 2D? 0 Answers
App gets stuck on WebGL 0 Answers