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
2
Question by conguerror · Jan 02, 2020 at 10:44 AM · transformscriptableobject

Scriptable object doesn't accept transform

So basically I can't drag parts of the player to the open(public) fields. The entire code would not work then. Picture easily explaining problem: alt text

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [CreateAssetMenu(fileName = "Weapon", menuName = "Weapons/Make New Weapon", order = 0)]
 public class WeaponScriptableObject :ScriptableObject
 {
     public Transform rightHandTransform = null;
     public Transform leftHandTransform = null;
     public AnimatorOverrideController animatorOverride = null;
     public float damage = 2f;
     public GameObject prefabOfWeapon = null;
     public bool isRightHanded = true;
     const string weaponName = "Weapon";
 
 
     public void Spawn
     (Transform rightHand,Transform leftHand,AnimatorOverrideController animatorOver)
     {
         DestroyPreviousWeaponEquipped(rightHand, leftHand);
         if(prefabOfWeapon != null)
         {
             Transform transformOfHands = HandTransformGetter(rightHand, leftHand);
             GameObject equippedWeapon = Instantiate(prefabOfWeapon, transformOfHands);
             equippedWeapon.name = weaponName;
         }
         else
         {
             Debug.LogWarning(weaponName + "Object doesn't have a prefab/model");
         }
         if (animatorOverride != null)
         {
             animatorOver.runtimeAnimatorController = animatorOverride;
         }
         else
         {
             Debug.LogWarning(weaponName + "Doesn't have an override. Please include it");
         }
 
     }
 
     void DestroyPreviousWeaponEquipped(Transform rightHand,Transform leftHand)
     {
         Transform oldWeapon = rightHand.Find(weaponName);
         if (oldWeapon == null)
         {
             oldWeapon = leftHand.Find(weaponName);
         }
         if (oldWeapon == null) return;
 
         oldWeapon.name = "DESTROYING";
         Destroy(oldWeapon.gameObject);
     }
   public Transform HandTransformGetter(Transform rightHand,Transform leftHand)
     {
         Transform handTransform;
         if (isRightHanded) { handTransform = rightHand; }
         else { handTransform = leftHand; }
 
         return handTransform;
     }
 }
 


capture.png (346.3 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Jan 02, 2020 at 12:47 PM

Of course that doesn't work. An asset can never reference objects that live in a scene. Scenes are not always loaded. Keep in mind that Scriptable Objects that are stored as asset are always "there" but the transform you want to reference is not. It only works the other way round.

Comment
Add comment · Show 4 · 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 conguerror · Jan 03, 2020 at 12:50 PM 0
Share

what do you suggest me to do? How can I get transform then?

avatar image TreyH conguerror · Jan 03, 2020 at 01:25 PM 1
Share

They aren't really meant to be used that way (inspector assignment of scene objects), but there's nothing stopping you from using an in-memory runtime instance of that object and assigning transforms that way.


 [CreateAsset$$anonymous$$enu(fileName = "Weapon", menuName = "Weapons/$$anonymous$$ake New Weapon", order = 0)]
 public class WeaponScriptableObject :ScriptableObject
 {
 
     public Transform rightHandTransform = null;
     public Transform leftHandTransform = null;
     // ...
 
     public void AssignHands(Transform right, Transform left)
     {
         this.rightHandTransform = right;
         this.leftHandTransform = left;
     }
 
     // ...
 }


with the usage being

 var weapon = ScriptableObject.CreateInstance<WeaponScriptableObject>();
 weapon.AssignHands(right, left);


but even then, you aren't even using those transforms on that object? The only time right or left hands come up at all are during your Spawn() call, where they just get assigned via arguments ins$$anonymous$$d.

avatar image conguerror · Jan 05, 2020 at 10:56 AM 1
Share

Thanks a lot.

avatar image conguerror · Jan 05, 2020 at 11:07 AM 1
Share

I just want to spawn a pickup object on hands. I am a bit new to coding, well trying as much as I can.

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

157 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 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

When camera is set to active: transform gameobject forwards from same transform position not working (help) 1 Answer

Weird Custom Editor bug 1 Answer

How to get local position & rotation from not parent object 1 Answer

Data with reference to scene objects: Monobehaviour or ScriptableObject? 1 Answer

C# 2d Always Rotate Facing Gameobject 1 Answer


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