#Create Instance
1 messages · Page 1 of 1 (latest)
I took a look at the unity doc, and it LOOKS like this should work, but Unity isn't having it.
[ContextMenu("Save Waypoints to Scriptable object")]
private void SaveWaypoints()
{
Path path = ScriptableObject.CreateInstance("Path");
for(int i = 0; i < waypoints.Count; i++)
{
path.waypoints.Add(waypoints[i].position);
}
}
and the error is or are we just supposed to guess?
sorry, got sidetracked
Cannot implicitly convert type 'UnityEngine.ScriptableObject' to 'Path'. An explicit conversion exists (are you missing a cast?)CS0266
so, you are not specifying what type of SO you want to Create
It's a Path SO.
so tell the code that
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Path", menuName = "StudioDMB/EZLerp/Path", order = 1)]
public class Path : ScriptableObject
{
public List<Vector3> waypoints;
}
Path knows what it is. CreateInstance does not know what Path is unless you tell it
And how do I tell CreateInstance what a Path is if a class definition is not enough?
you have not given CreateInstance a class definition
Pretend I don't know how to do that.
but you do know, you do the same thing with your List declaration
https://docs.unity3d.com/6000.0/Documentation/ScriptReference/ScriptableObject.CreateInstance.html
all the unity doc says is to specify the SO I want to make.
I have created the SO, defined it's parameters, told ScriptableObject.CreateInstance what class I want it to create an instance of.
ScriptableObject ScriptableObject.CreateInstance(string className) (+ 2 overloads) Creates an instance of a scriptable object. is what I get from Visual Studio.
and it LOOKS like that is exactly what I have given it.
try the other overload and pass in
typeof(Path)
same result
you should also be able to use the Generic
CreateInstance<Path>
aaah
Okay, it accepted the generic. Thank you.
It would be even better if Unity's documentation told me that.
It doesn't say that.
nor does it say to specify a type as a generic.
It says to pass it as a parameter.
aaah
This is for a garden variety Scriptable Object
It must be a convention thing. I have never seen generics written like this.
I have seen something along the lines of public static ScriptableObject CreateInstance<T>();
like LinkedList<T>
or BinaryTree<T>