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 DeOne · Jan 30, 2015 at 01:13 PM · script error

Object reference not set to an instance of an object error.

Baisicly I have deleted 99% of unnececary code, but this error "Object reference not set to an instance of an object error". is occouring in Test script, You can find with "ERROR SHOWS HERE" I have spend multiple hours to find similar cases in unity answers, I dont understand why it does not work. Why I cant launch StartBuff from another script via find object, get the script and launch StartBuff with these values...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 
 public class StatusEffect : MonoBehaviour
 {
     public int MovePowerModif;
     public int TurnPowerModif;
     public int StrModif;
     public int AgiModif;
     public int IntModif;
     public int FortModif;
     public int WillModif;
     public List<Buff> BuffList;
     
     // Test if list works.
     public void BuffManager()
     {
         if (BuffList.Count != 0) 
         {
             foreach (Buff buff in BuffList) 
             {
                 Debug.Log (buff.EffectName); // test
             }
         }
         else
         {
             Debug.Log("Buff list empty.");
         }
     }
     
     public void StartBuff(string NewEffectName, int NewEffectPower, string NewEffectStat, int NewEffectDurration, string NewEffectDescription)
     {
         string EffectName = NewEffectName;
         int EffectPower = NewEffectPower;
         string EffectStat = NewEffectStat;
         int EffectDurration = NewEffectDurration;
         string EffectDescription = NewEffectDescription;
         BuffList.Add (new Buff (EffectName, EffectPower, EffectStat, EffectDurration, EffectDescription));
         BuffManager();
         StartCoroutine(BeginCountdown(EffectDurration));
     }
     IEnumerator BeginCountdown (int Durration)
     {
         yield return new WaitForSeconds (Durration);
         Debug.Log ("Buff Ends");
     }
 }
 public class Buff
 {
     public string EffectName;
     public int EffectPower;
     public string EffectStat;
     public float EffectDurration;
     public string EffectDescription;
     
     public Buff(string NewEffectName, int NewEffectPower, string NewEffectStat, int NewEffectDurration, string NewEffectDescription)
     {
         EffectName = NewEffectName;
         EffectPower = NewEffectPower;
         EffectStat = NewEffectStat;
         EffectDurration = NewEffectDurration;
         EffectDescription = NewEffectDescription;
     }
     
 }


//This is another script in another file. using UnityEngine; using System.Collections;

 public class TestScript : MonoBehaviour {
 
     void Start()
     {
         GameObject Hero = GameObject.Find ("Hero");
         StatusEffect HeroStatScript = Hero.GetComponent<StatusEffect>();
         HeroStatScript.StartBuff ("Slow", 10, "Str", 3, "Ur slowed"); ERROR SHOWS HERE!!!
     }
 }
 
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

1 Reply

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

Answer by hav_ngs_ru · Jan 30, 2015 at 04:12 PM

if you want to use script as Component (attach it to GameObjects and use them via GetComponent) the name of component class must be the same as filename.

here you tried to declare two nested components in one file. Of cause, GetComponent couldnt retrive StatusEffects as component.

if you want to incapsulate some data and baheviour in StatusEffects (instead of storing it directly in HeroStats members) - do it, DONT derive StatusEffects from MonoBehaviour.

 public class HeroStats : MonoBehaviour
 {
    public class StatusEffects {
       public void StartBuff(string NewEffect() {
          ...
       }
    }
    
    public StatusEffects effects = new StatusEffects();
 }
 
 
 ...
 
 GameObject Hero = GameObject.Find ("Hero");
 HeroStats HeroStatScript = Hero.GetComponent<HeroStats>();
 HeroStatScripteffects.StartBuff (EffName, EffPow, StatAff, Durr, Descpt);
 
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 DeOne · Jan 30, 2015 at 05:03 PM 0
Share

I tryed to understand ,but what do you mean by "HeroStatScripteffects.StartBuff" this part? HeroStatScripteffect doesnt exsist.? I even splited HeroStats from StatusEffect classes in diffirent files, ill update the 1st question... But changeing it didnt helped a bit tho, same error.

avatar image hav_ngs_ru · Feb 01, 2015 at 03:24 AM 0
Share

well, post a new code now, after you changed it - I dont know haw it looks.

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

20 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

Related Questions

all my scripts stopped working after i put a enemy spawner script in 0 Answers

HostListReceived symbol resolve 1 Answer

error CS1525: Unexpected symbol 'rigidbody' 1 Answer

C# script works wrong ( GUI ) How to fix it ? 1 Answer

Please. Help me out :( 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