- Home /
Null variable
Hi folks,I have a little problems that's drive me in crazy. When I instantiate dinamically some gameObjects, I add to it a route and I want specify that route on a script named getRota. But I isn't happening. When I test my application, they haven't a route (getRoute.route=null). Someone can help me with this?
getRoute.cs:
public class getRoute : MonoBehaviour {
public MonoBehaviour route;
}
instantiateNPCwandering.cs:
void Start () {
GameObject gmO=this.gameObject;
while(loop)
{
if(toClone!=null)
gmO=Instantiate(toClone,transform.position+OnUnitCircle()*2,Quaternion.identity) as GameObject;
gmO.AddComponent ("wanderCircle");
if(!gmO.GetComponent<getRoute>())
gmO.AddComponent ("getRoute");
gmO.GetComponent<getRoute>().route=(MonoBehaviour)gmO.GetComponent<wanderCircle>();
howMany++;
if(howMany==total)
loop=false;
}
}
getRoute must not inherit from $$anonymous$$onoBehaviour.
why not? this was the way I found so others gameObjects know what route these objects have and can enable it or disable it
because is a gameObject component, so is a $$anonymous$$onoBehaviour type
Answer by Ninita · May 10, 2013 at 02:36 PM
recently I have to ask again about this (http://answers.unity3d.com/questions/451780/problem-in-assigning-a-random-route.html) and I already solved this. thanks for all
Answer by Yokimato · Apr 16, 2013 at 12:31 PM
It looks like you may need to go back to the drawing board with this. You have the right idea, but I feel you haven't implemented it the best you could. I would have a NpcPath
(or something like that) mono that encapsulates the state of the route, which route they're currently doing, the ability to change that route...etc etc..
The design you have is too broken up and too small. You almost always see people do the exact opposite and throw too much code in one class rather than the other way around. The problem with yours is that you need multiple components to execute 1 idea. And that too violates the Single Responsibility Principle, only its on the unusually seen side of the spectrum.
A good rule of thumb: If you're fighting your design constantly, you should step back and reevaluate it to see if it still fits the problem space.
Initially I implemented an FS$$anonymous$$ where I had to interact with a NPC, etc.. But when expanded the project to various NPC, ins$$anonymous$$d of making a npc = GameObject.FindGameObjectWithTag ("NPC") I had to make a npc = GameObject.FindGameObjectsWithTag ("NPC"). So before there was no problem because I defined manually which route the NPC have, but when I create clones I have to set these routes by scripting
Why not make that an enum and pick one as default...or on start pick a random one for that instance (or clone).
later I'll try that. But I don't understand why this don't work
@Ninita, with enough time and motivation, you can get anything to "work", but you have to ask yourself if it's maintainable or flexible so that you're able to trouble shoot when need be and scale your application when need be.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100421im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
pass transform value to another script 1 Answer
Associate objects to a prefab 1 Answer
Referencing gameObject from script after Instantiate 0 Answers
How can I get the position of transforms inside an instantiated game object? 1 Answer
Object reference to set to instance on an instantiated object 1 Answer