- Home /
GameObject references runtime script in scene file. Unsure whats wrong
Keeps dropping me an error while trying to read this script and I'm unsure why. Trying to use this as a class
using UnityEngine;
[System.Serializable]
public class PlayerWeapon{
public string weaponName = "Pistol";
public float damage = 10f;
public float range = 100f;
}
Just make a normal script with a empty class that inherits from $$anonymous$$onoBehavior. Then above the empty class you can write the code you have now and it should work. I'll write the example below.
Answer by KittenSnipes · Jan 26, 2018 at 10:44 AM
Well it seems like you are trying to make a scriptable object. These are very useful when doing exactly what you are doing. This is a video from one of my favorite YouTubers who gives a very good explanation of how to use a scriptable object: Scriptable Object Video
But if you do not wish to do so here is a script which certainly should fit your needs:
using UnityEngine;
//Now when you right click in your project tab and click new you will see your script
//The fileName will be the name of your object if you do not set it
//The menuName will be the name you see when you want to create a new object
[CreateAssetMenu(fileName = "New Weapon", menuName = "Weapon")]
//The ScriptableObject part is very important because it allows you to create this object
public class PlayerWeapon : ScriptableObject
{
//Sets your weapon name
public string weaponName = "Pistol";
//That damage doh.
public float damage = 1000000f;
//What range ;)
public float range = 0.0000001f;
}
Answer by JusSumGuy · Jan 29, 2018 at 05:51 AM
using UnityEngine;
[System.Serializable]
public class PlayerWeapon {
public string weaponName = "Pistol";
public float damage = 10f;
public float range = 100f;
}
public class ScriptYouAlreadyHave : MonoBehaviour {
// existing code
}
//
//
You can just add the class to a script you have already written, and it should work.
And if you want to add the script to more than one object, you can add your class to a script with a empty class and just make the empty class inherit from $$anonymous$$onoBehaviour.
Your answer
Follow this Question
Related Questions
Create a scene from script 2 Answers
How would I search my scene for a tag 1 Answer
How to add a Script to a GameObject during Runtime [2016] 3 Answers
How to access a game object from a different scene? 0 Answers
How do I play gameobjects in my scene from one script / Script is not working 0 Answers