- Home /
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:
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;
}
}
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.
what do you suggest me to do? How can I get transform then?
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.
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
