- Home /
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
}
}
}
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.
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
Your answer
Follow this Question
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