- Home /
How do I pass an object to another script?
It's been a while (a few years, actually) since I picked up Unity and I'm trying to get back into it. From what I've seen so far, #pragma strict
appears to be all the rage lately. However, it apparently doesn't use dynamic typing, which I think is the root of my issue. I'm simply trying to pass an instance of a class from one script to another. However, I'm getting this error:
'name' is not a member of 'Object'
When I attempt to run this code:
In Item.js:
function OnTriggerEnter(other : Collider) {
if(other.gameObject.CompareTag("Player")) {
var weapon = new PlayerItem("Test Weapon Name");
other.SendMessage("AddItemToInventory", weapon);
}
}
In PlayerInventory.js:
class PlayerItem {
var name : String;
function PlayerItem(g_name : String) {
name = g_name;
}
}
function AddItemToInventory(itemProperties) {
Debug.Log(itemProperties.name);
}
Where did I go wrong? UnityScript today isn't the UnityScript I remember. Heh.
I Don't think you should use the new keyword. But I'm not a unityscripter.
Okay, I tried my script without the new
keyword. No difference.
Answer by aldonaletto · May 20, 2012 at 03:13 AM
You should define the parameter type of itemProperties:
function AddItemToInventory(itemProperties: PlayerItem){
SendMessage passes an object type (variant) to the called function, thus it can be anything - you must specify which's the actual parameter type.
Your answer

Follow this Question
Related Questions
Null Reference - Help 0 Answers
Network Respawn Object not set... 0 Answers
Confusing Object Reference Error 0 Answers
NullReferenceException: Object reference not set to an instance of an object 2 Answers
What is wrong with my C# script? 2 Answers