Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ninita · Apr 16, 2013 at 09:19 AM · instantiatenullreferenceexceptionmonobehaviour

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;
                     }        
                 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lockstep · Apr 16, 2013 at 10:50 AM 0
Share

getRoute must not inherit from $$anonymous$$onoBehaviour.

avatar image Ninita · Apr 16, 2013 at 11:38 AM 0
Share

why not? this was the way I found so others gameObjects know what route these objects have and can enable it or disable it

avatar image fafase · Apr 16, 2013 at 11:58 AM 0
Share

Why a $$anonymous$$onoBehaviour type?

avatar image Ninita · Apr 16, 2013 at 12:07 PM 0
Share

because is a gameObject component, so is a $$anonymous$$onoBehaviour type

avatar image Linus · Apr 16, 2013 at 12:25 PM 0
Share

What is the exact error message?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ninita · Apr 16, 2013 at 12:49 PM 0
Share

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

avatar image Yokimato · Apr 16, 2013 at 01:13 PM 0
Share

Why not make that an enum and pick one as default...or on start pick a random one for that instance (or clone).

avatar image Ninita · Apr 16, 2013 at 03:25 PM 0
Share

later I'll try that. But I don't understand why this don't work

avatar image Yokimato · Apr 16, 2013 at 05:46 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges