Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by privatas98 · Jan 25, 2018 at 09:38 PM · gameobjectscript.sceneruntimereferencing

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

alt text alt text

 using UnityEngine;
 
 [System.Serializable]
 public class PlayerWeapon{
 
     public string weaponName = "Pistol";
 
     public float damage = 10f;
     public float range = 100f;
 
 }


error1.png (7.8 kB)
error2.png (11.6 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JusSumGuy · Jan 29, 2018 at 05:58 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KittenSnipes · Jan 26, 2018 at 10:44 AM

@privatas98

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;
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JusSumGuy · Jan 29, 2018 at 05:55 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges