Question by
YungCake · Aug 14, 2016 at 08:32 AM ·
c#scripting problem
C# Script cant find Tagged Object
I have a script that im making to change the colour of a ball when the player touches it, I have tagged the GameObject with a tag called ObjectMovable. The script, no matter how much I try can never find it.
using UnityEngine;
public class ScriptforCollision : MonoBehaviour
{
public GameObject[] ObjectMovable;
public void Start()
{
GameObject.FindWithTag("ObjectMovable");
}
public void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
if (collision.relativeVelocity.magnitude > 2)
{
GameObject.ObjectMovable.renderer.material.color = Color.red;
}
}
}
Please help me fix my script, I cant seem to get it to find the tag "ObjectMovable which is on a Game Object
error.png
(3.9 kB)
tag-not-working.png
(5.5 kB)
Comment
Answer by G4merSylver · Aug 14, 2016 at 09:54 AM
GameObject.FindWithTag("ObjectMovable");
May I inquire what you are doing with this? You call the function which returns a gameobject, but you dont store it anywhere? Also why are you using an array that should give an error to?
public GameObject ObjectMovable;
[...]
ObjectMovable = GameObject.FindWithTag("ObjectMovable");
[...]
ObjectMovable.renderer.material.color = Color.red;
Your answer
![](https://koobas.hobune.stream/wayback/20220612090800im_/https://answers.unity.com/themes/thub/images/avi.jpg)