Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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 2 · 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, 2021 at 02:42 AM 0
Share

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

avatar image davidnibi Olakehs · May 20, 2021 at 05:34 PM 0
Share

There's nothing wrong with doing it this way in Start() - you could just create a variable and put the object with the script on it in the slots, but it's 2021, don't worry about finding something on start up if you have to.

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

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

Related Questions

Switch Statement Gives Error PLEASE HELP!!! 2 Answers

Active and Inactive Spawn Points 0 Answers

Changing a value in a C# script in a list Objects 1 Answer

I Keep Getting Error CS0052 1 Answer

Restrict amount of multiple choices per Enum 0 Answers


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