• 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 /
  • Help Room /
avatar image
2
Question by drizztdourden_ · Sep 05, 2016 at 04:16 AM · enumanother script

Changing enum state via another script

Hello,

I've been struggling in these forum / answers for an answer to this for a couple of hours now but nothing seems to work for me for some reason. Even though some answer sound logical, I can't make it work.

Here's my code, which based of some other answer, should work:

Separate Script for the enum

 public enum CharacterState
 {
     Walking,
     Dialog,
     Combat
 }

CharController Script

 public class mCharWorld : MonoBehaviour
 {
 
 public CharacterState CharState;

 void Start()
 {
     CharState = CharacterState.Walking;
 }

 void Update()
 {
     if (CharState == CharacterState.Walking)
     {
          debug.log("Something");
     }
 }

 }

In Another Script

 public mCharWorld mCharScript;

 public void InitializeDialog()
 {
     mCharScript.CharState = CharacterState.Dialog;
 }


This last script only throw the classic exception "NullReferenceException: Object reference not set to an instance of an object".

Not sure how it can be null at this point as it would work for any other type of variable.

A bit of help would be appreaciated.

Best regards,

Comment
Add comment
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

2 Replies

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

Answer by drizztdourden_ · Sep 05, 2016 at 03:48 PM

Stupid mistake: It was just not initialized when I though it was done. For those who would get something similar:

 mCharWorld mCharScript;

 void Start(){
      mCharScript = GameObject.Find("MainChar").GetComponent<mCharWorld>();
 }

 void WhatEver(){
     mCharScript.CharState = CharacterState.Dialog;
 }

EDIT: You can also obviously make the variable "public" and attach it in the editor for more performance depending on your needs.

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
1

Answer by andreadoria · Nov 28, 2020 at 10:56 AM

I would suggest you to never use GameObject.Find() in Start() function. You can have really bad performance issues on many devices. Not a very clean solution even for one object or for one frame. There are other methods way more efficient and code clean to do the same.

Unity stores all game objects in a scene within a graph data structure. Finding a game object using Find functions requires performing a depth-first search. Performing this search means looking through the entire graph. This graph contains every game object in every scene open in the game.

A depth first search is an algorithm that has O(V+ E) complexity. What this means is that as our graph grows by adding more vertices and edges, it can take longer for us to search through the graph. The more game objects in the scene, the longer it can take for us to find the game object. Game objects further down in the graph will also take longer to find.

Comment
Add comment · Show 1 · 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 Olakehs · Mar 13 at 02:42 AM 0
Share

Thanks for your point. Would you be able to refer to some examples of better ways?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

69 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I set one enum value equal to another enum value? 2 Answers

Problem assigning enum value 1 Answer

inspector dropdown (or enum) based on scriptable object custom class 0 Answers

Clean ways of setting up world data, Structs v Classes v Enums etc. 2 Answers

if statements using enums (solve) 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