- Home /
Problems with Hashtables and creation order?
I am having some problems initializing and calling my hash table of hash tables. From what I can see I am creating the tables correctly, but when I try to access the hashtable from another script in the start menu, it says that the object reference isn't set.
When the same is called from the other scripts update, everything is fine. So is there a way to prioritize the creation of the hashtable before the other script calls its Start()?
I'm clearly missing something, so if somebody can point me in the right direction, that would be great.
Here's my class with the Hashtable:
using UnityEngine; using System.Collections;
public class PathLibrary : MonoBehaviour {
public Hashtable thePaths;
// Use this for initialization
void Start () {
thePaths = new Hashtable();
initPaths();
Hashtable currentRoute = (Hashtable)thePaths["Path1"];
Vector3[] thePoints = (Vector3[])currentRoute["PathPoints"];
print("First Coord:" + thePoints[0].ToString());
}
void initPaths(){
Hashtable ht = new Hashtable();
//Path1 Init
ht = iTween.Hash("PathPoints", new Vector3[]{v3(0,0,10), v3(15,0,40)},"Speed", 2.0f);
thePaths.Add("Path1", ht);
//Path2 Init
ht = iTween.Hash("PathPoints", new Vector3[]{v3(0,0,10), v3(15,0,40)},"Speed", 2.0f);
thePaths.Add("Path2", ht);
//Path3 Init
ht = iTween.Hash("PathPoints", new Vector3[]{v3(0,0,10), v3(15,0,40)},"Speed", 2.0f);
thePaths.Add("Path3", ht);
Hashtable currentPath = (Hashtable)thePaths["Path1"];
print ("The Route Points" + ((Vector3[])currentPath["PathPoints"])[0].ToString());
}
Vector3 v3(float x, float y, float z){
return new Vector3(x,y,z);
}
}
And here is the class that calls the hashtable
using UnityEngine;
using System.Collections;
public class CallPath : MonoBehaviour {
PathLibrary pathLib;
void Start () { pathLib = GetComponent<PathLibrary>();
Hashtable currentRoute = (Hashtable)pathLib.thePaths["Path1"];
Vector3[] thePoints = (Vector3[])currentRoute["PathPoints"];
print("First Coord nenenene:" + thePoints[0].ToString());
/*This causes an error message that reads "NullReferenceException:Object reference
not set to an instance of an object
*/
}
void Update () {
Hashtable currentRoute = (Hashtable)pathLib.thePaths["Path1"];
Vector3[] thePoints = (Vector3[])currentRoute["PathPoints"];
print("First Coord nenenene:" + thePoints[0].ToString());
//This works!!!!
}
}
Answer by Peter G · Dec 01, 2010 at 12:41 AM
I haven't tested you code, but if all's you need to do call 1 script 1st, then you can put the 1st one in the Awake() method which is called before Start().
Thanks so much! I knew it must have been something simple.
Thank You! You just fixed a problem that I've been trying to fix for 4 hours :)
Answer by pixelplacement · Dec 03, 2010 at 07:54 AM
Would this help you? http://pixelplacement.com/2010/12/03/visual-editor-for-itween-motion-paths/
Wow! That is really a fantastic tool. Unbelievable job and I would love to use it. The only problem is that I really need to animate between the points in a linear fashion without the Catmull-Rom splines. So I ended up using iTween, but breaking down each path into X number of animations.
If there is a way to use this without the CatmullRom spline please let me know.
Really cool stuff man.
Your answer
Follow this Question
Related Questions
Hashtable and ArrayList problem 0 Answers
Can I make a list of hashtables or classes with pragma strict 2 Answers
Most efficient way to store a list of data as a string? 1 Answer
How to check execution order from script? 0 Answers
Accessing quadrant/joystick number when using multiple xbox 360 controllers 1 Answer