- Home /
Instantiated objects accessability
Hello.
In my game, I can "shoot" balls with instantiate function in the script, that is attached to my Character Controller.
To Character Controller is attached another script, which Im trying to use to check if the ball hit the player. I have 2 versions, but they dont work. I think its because of I can do this check only to original Ball and not to its copies. But I dont know how to access them :(
First:
void OnControllerColliderHit(ControllerColliderHit hit) {
if(hit.gameObject.name == "Gule")
print("trolololololl");
}
Second:
public Transform Gule;
void OnControllerColliderHit(ControllerColliderHit hit) { if(hit.gameObject == Gule) print("trolololololl"); }
Thank you for your ideas! Erhan
Gule means "Ball" and "trololololl" is just my check message ^^
Answer by Jesse Anders · Nov 13, 2010 at 03:21 PM
Try using tags instead.
'It didn't help' doesn't tell us anything. You haven't told us what changes you've made, so we don't know if your new code is correct, if you've assigned the appropriate tags to the right objects, etc. In other words, you're making it very hard for us to help you :) In any case, the problem (as you seem to suspect) is probably that the clones are not named "Gule". The tag, however, will be the same for any clones as it is for the original, which is why I suggested using tags ins$$anonymous$$d. If that's not working for you, please edit your post to include more information so that we can help you.
Im so noob. Sorry, but I tried to add a tag in code. I realised now, that it would be better to add it in inspector and voila! Thank you! :)
Answer by Loius · Nov 13, 2010 at 04:29 PM
Every object is different. Storing a reference to one ("public Transform gule") only references that specific one.
When an object is created through Instantiate(), (Clone) is added to its name.
The best way to do this is to give the balls a unique tag or a unique component. You can then use
if ( hit.gameObject.tag == "Tag" )
or
if ( hit.gameObject.GetComponent( ComponentName ) )
Your answer
Follow this Question
Related Questions
How to access varibles on a newly instantiated object 2 Answers
Access Child of instantiated UI-Prefab using C# in Unity Beta 4.6 1 Answer
access instantiated object variable 0 Answers
accessing an instantiated prefab's components in C#? 2 Answers
Access a instantiated gameobject from a enemy spawner 4 Answers