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 sqil · Apr 20, 2015 at 07:20 PM · 2derrorunity5variableinstance

UI Image problem, not set to an instance of object

Im working on a game for my 1st year University project, this code works until the player dies, and it's clone respawns (without the public UI Images set in the inspector).

After trying to change the 'stance' console throws hundreds of errors that say "Object reference not set to an instance of an object".

Is anything wrong with this code? I was thinking it might be an issue because I'm not telling the game that the players clone is player and the images should attack to it automatically on respawn.

I had a look at the error online, but it did not quite understand it. (be easy on me i'm kind of new to this) Thanks in advance!

         using UnityEngine;
         using System.Collections;
         using UnityEngine.UI;
     
         public class StanceChanger : MonoBehaviour
         {
         
         float Stance = 0f;
         Animator anim;
         private GameObject monkeyArm;
         // Use this for initialization
     
         public Image Main;
         public Image Bear;
         public Image Cat;
         public Image Monkey;
         
         public Sprite ActiveMainIcon;
         public Sprite ActiveBearIcon;
         public Sprite ActiveCatIcon;
         public Sprite ActiveMonkeyIcon;
     
         public Sprite InactiveMainIcon;
         public Sprite InactiveBearIcon;
         public Sprite InactiveCatIcon;
         public Sprite InactiveMonkeyIcon;
     
         void Awake()
         {
             monkeyArm = GameObject.FindGameObjectWithTag("MonkeyArm");
             monkeyArm.GetComponent<Renderer>().enabled = false;
         }
         void Start()
         {
     
             anim = GetComponent<Animator>();
         }
     
         // Update is called once per frame
         void Update()
         {
             
                   if(Stance != 0)
                 {
                     Main.sprite = InactiveMainIcon;
                 }
                 if(Stance != 1)
                 {
                     Bear.sprite = InactiveBearIcon;
                 }
                 if(Stance != 2)
                 {
                     Cat.sprite = InactiveCatIcon;
                 }
                 if(Stance != 3)
                 {
                     Monkey.sprite = InactiveMonkeyIcon;
                     monkeyArm.GetComponent<Renderer>().enabled = false;
                 }
     
             // No Wings Stance
             if (Input.GetKeyDown(KeyCode.Alpha1))
             {
                 Stance = 0f;
                 anim.SetFloat("StanceChange", Stance);
                 Main.sprite = ActiveMainIcon;
     
             }
             // Bear Stance
             if (Input.GetKeyDown(KeyCode.Alpha2))
             {
                 Stance = 1f;
                 anim.SetFloat("StanceChange", Stance);
                 Bear.sprite = ActiveBearIcon;
             }
             // Cat Stance
             if (Input.GetKey(KeyCode.Alpha3))
             {
                 Stance = 2f;
                 anim.SetFloat("StanceChange", Stance);
                 Cat.sprite = ActiveCatIcon;
     
             }
             //Monkey Stance
             if (Input.GetKey(KeyCode.Alpha4))
             {
                 Stance = 3f;
                 anim.SetFloat("StanceChange", Stance);
                 Monkey.sprite = ActiveMonkeyIcon;
                 monkeyArm.GetComponent<Renderer>().enabled = true;
     
             }
             //Attack
             if (Input.GetKeyDown(KeyCode.F) && Stance == 1f)
             {
     
                 Invoke("Attack", 1); // Invoking attack = slow attack time for animation
             }
         }
     
     }
 

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
1

Answer by zeppike · Apr 21, 2015 at 01:10 PM

You have to set your Prefab up with references pointing only components of the prefab. I guess your problem is after destroying the player, you loose all the outer references you set up in the inspector.

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 sqil · Apr 21, 2015 at 08:54 AM

OK I solved it. I just had to copy the canvas under the Player GameObject that would get instatniated on respawn... can't believe it was this simple

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

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

Updating a variable on a script in an instanced object 1 Answer

How can a character not move out of box collider's bounds? 1 Answer

Unity 5 - submeshes don't work as before 1 Answer

Player sinking into walls and floor with Raycasts 1 Answer

error CS0841: A local variable `holdSound' cannot be used before it is declared 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