- Home /
Why are these scripts getting different icons and could it have anything to do with my problem?
Trying to get a script talk to another script..
Hi i have a problem with my project that i hope someone here could help me with. I got a script that handles an animation on a 2d-plane. The script looks like this:
using UnityEngine;
using System.Collections;
public class Animation : MonoBehaviour
{
public int columnSize;
public int rowSize;
Vector2 size;
Vector2 offset;
int index;
public void [B]Animate[/B](float rowFrameStart, int colFrameStart, int totalFrames, float framesPerSecond, bool isNegative)
{
size = new Vector2(1.000f / columnSize, 1.000f / rowSize);
index = (int)(Time.time * framesPerSecond);
//index %= (columnSize * rowSize);
index %= totalFrames;
int u = index % columnSize;
int v = index / columnSize;
offset = new Vector2((u + colFrameStart) * size.x, (1 - size.y) - (v + rowFrameStart) * size.y);
if (isNegative)
{
offset = -offset;
}
SetScaleAndOffset(offset, size, true);
}
private void SetScaleAndOffset(Vector2 offset, Vector2 size, bool haveNormalMap)
{
renderer.material.mainTextureOffset = offset;
renderer.material.mainTextureScale = size;
if (haveNormalMap)
{
renderer.material.SetTextureOffset("_BumpMap", offset);
renderer.material.SetTextureScale("_BumpMap", size);
}
}
}
This script is attached to any objects that needs to have an animated material, the animation is then controlled by another script calling the method Animate. The code that does this is these lines, (or they should do it):
private Animation animation;
void Start ()
{
animation = gameObject.GetComponent("Animation") as Animation;
}
void Update ()
{
animation.Animate(0, 0, 16, 20, false);
}
So both these scripts are attached to the same gameobjects. But when i try to test it out i get:
NullReferenceException: Object reference not set to an instance of an object
In the second script when i try to find a reference to the first script to run the method Animate something is wrong. animation = gameObject.GetComponent("Animation") as Animation; is´nt doing what it should. animation is equal to null, why? In the unity editor the two scripts got different icons, could it have something to do with that?
I made a picture to show you what i mean here
I hope you guys got an answer to my question.
EDIT * I managed to fix it by adding the animation script through the other script, so that i get a reference to it right away. But i still don´t get why the scripts got different icons..
Answer by delstrega · Jul 06, 2012 at 11:51 AM
If a script gets a different icon it is a good sign that there's a class with the same name used by Unity. http://docs.unity3d.com/Documentation/ScriptReference/Animation.html
Those icons are assigned by name and you can simulate that by creating a folder "Assets/Gizmos/" and putting a .png file " Icon.png" there (for example MySimpleAnimation Icon.png). Now every script with that class name will get this icon.
Animation is a unity internal class so you won't find a "Animation Icon.png" anywhere, but behind the scenes that's what happens there to: Your class is named "Animation" so it gets the icon.
EDIT: I guess Unity just gets confused since there's two possible "Animation" classes here. Try naming your classes more uniquely.
Answer by zoorroo4 · Jul 06, 2012 at 01:49 PM
Oh right unity also has a class named Animation, how silly of me not to notice. Thanks for the help :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613073753im_/https://answers.unity.com/themes/thub/images/avi.jpg)