- Home /
SimplePath instantiated AI
I just purchased SimplePath from the Unity asset store and I've hit a snag: When instantiating an enemy who has a PathAgentComponent I am immediately setting the m_pathManager to the scene's PathManager but still getting errors and no pathing behavior. The pathing works great if the enemy already exists in the editor but I'm having trouble getting enemies instantiated at runtime to work. Is there a solution to this?
edit
Since SimplePath is a system I paid for I don't think I'm allowed to post the code here but I think the root of the issue is needing to set a property on a behaviour after instantiation but before Awake
is called. Is that possible or is there a common approach for this type of issue that I am just unfamiliar with?
would it be possible for you to contact the developer of SimplePath? I guess we wont see the inside code of it here, so I find it a bit hard to answer.
I tried to find his email address but never did, I was hoping he would see this post. Since it's a paid library I can't post the code :/ (not sure of the exact legal rules here but I'm gonna play it safe and not post any of the code)
also see edit
Tanner did you ever resolve these issues? If so how did you? I can't seem to get an instantiated actor $$anonymous$$oveToPosition() successfully yet it works for a GameObject already in the scene.
Tanner I'm waiting on the developer to look at a simple instantiate problem I'm having with SimplePath. When he tells me what I'm doing wrong in the scene I'll post a download link here.
Answer by whydoidoit · May 14, 2012 at 01:10 AM
I'm pretty sure you can't do something before Awake - however, how about you get the prefab - change the variable in there and then do an Instantiate on it? That would mean the value was set before you instantiated the copy.
Given that instantiate just makes a copy of a GameObject - you have the GameObject that is your prefab, you can just change that in the code on the line before the Instantiate and then Instantiate will make a copy using the updated value.
Answer by whydoidoit · May 14, 2012 at 08:45 AM
To expand on my previous answer (which I'm doing because this does solve the general problem of setting a variable that needs to be used in Awake or changed before it is called for some reason).
Imagine you have a prefab that contains a reference to another object that is used in Awake:
using UnityEngine;
using System.Collections;
public class DoSomethingInAwake : MonoBehaviour {
public GameObject otherObject = null;
void Awake ()
{
print (otherObject.name);
}
}
That is attached to a prefab - let's say it has null in the otherObject. Clearly this raises an exception when it is instantiated.
The following code could be used to initialise the value of otherObject in advance:
public class InstantiateSomething : MonoBehaviour {
public Transform prefab;
// Use this for initialization
void Start ()
{
//add these lines to update the value before instantiation
var dsia = prefab.GetComponent<DoSomethingInAwake> ();
dsia.otherObject = GameObject.Find ("Main Camera");
//instantiate the updated prefab
GameObject.Instantiate (prefab);
//prints "Main Camera"
}
}
In your case you would change the GetComponent to return the path agent and set m_pathManager to your current path manager.
Thank you $$anonymous$$ike. I was having the same issue as the original post, and using this method I was able to get my problem fixed. $$anonymous$$y characters are now instantiating and using SimplePath correctly.
Your answer
