I'm having a problem with shooter script. Unity in this section shows me the following error:
'transform' is not member of 'Object'
I tried to repair it, but it doesn't work. Any solution?
function ApplyDamage(dmg : float, hit)
{
hit.transform.SendMessage("Damage",dmg);
}
The most common error here is to assume that a generic Object is a GameObject one (that is, a subtype).
I would try one of these two approaches:
1) Defining hit as a GameObject directly in the parameter's definition:
function ApplyDamage(dmg : float, hit : GameObject)
{
hit.transform.SendMessage("Damage", dmg);
}
2) Accessing the gameObject component of it:
function ApplyDamage(dmg : float, hit)
{
hit.gameObject.transform.SendMessage("Damage", dmg);
}
Related
So the title is a bit weird but i didnt know how to call it.
I am working on a FPS game and I am trying to make a simple weaponsystem. Every Player can have a primary and a secondary weapon. I am at the moment trying to write a script to change between the assigned primary/secondary weapons.
So at first I am doing this:
var primary : GameObject;
var secondary : GameObject;
So I have some GUI Buttons that when they get clicked they assign the desired weapon to the variables primary/secondary.
An code example:
function assignump45() {
primary = ump;
}
Now I want to write a function to switch between the primary and secondary weapon.
So I tried this:
function switchtoprimary(){
if(Input.GetKeyDown("2")){
primary.inv(); //makes the primary weapon invisible
secondary.vis(); //makes the primary weapon visible
}
}
Of course I get this error:
BCE0019: 'inv' is not a member of 'UnityEngine.GameObject'.
I know that what I wrote is wrong. So I tried to get the script of the primary/secondary weapons so I can disable/activate them:
var primscipt : umpscript = GameObject.Find(ump).GetComponent(umpscript);
This works BUT I can´t write for every weapon this kind of script because I then I need to write several combinations of switching between the weapons and that isn´t possible because i know there is a better solution..
I can´t do a if clause and then assign the primscript because the variable only would be assigned in the if clause..
What I need is something like this (doesn´t work of course^^).
var primscipt : primaryscriptstring = GameObject.Find(primarystring).GetComponent(primaryscriptstring);
So I could assign the variable primaryscriptstring with "umpscript" for example. the variable primarystring does work in this case
Are there any workarounds? I am pretty desperate at the moment :/
You'll probably want to create a Weapon class, and have your primary and secondary vars be Weapons, not GameObjects.
var primary : Weapon;
var secondary : Weapon;
Your Weapon.js file might look something like:
function vis () {
...
}
function inv () {
...
}
Then assuming you want to have every weapon type be an extended class of Weapon, you might create a UMP.js file for example and have its class extend Weapon like so:
#pragma strict
class UMP extends Weapon {
}
UMP then inherits all functions and vars from the Weapon class. So then, elsewhere in your code, the following line would work (assuming there's a GameObject named "UMP" somewhere that has the UMP component attached):
function assignump45() {
var ump : UMP = GameObject.Find("UMP").GetComponent(UMP);
primary = ump;
primary.vis();
}
Alternatively, you might not want to have every weapon type be an extended class, and just have one Weapon class for all weapons, in which case you wouldn't create a UMP.js file, and instead your code might look like:
function assignump45() {
var ump : Weapon = GameObject.Find("UMP").GetComponent(Weapon);
primary = ump;
primary.vis();
}
Instead of
primary.inv();
secondary.vis();
do
primary.SetActive(false);
secondary.SetActive(true);
The SetActive method works for any game object, so you don't have to worry about the specific type of weapon or script. See the Unity docs on this here.
I often need to pass invalid location to functions that optionally can use location (eg. RTS unit action). But this does not compile:
public abstract void CastSpell(Spell spell, Vector3 targetLocation = null);
So how can I make a proper invaid Vector3 to determine invalid/"don't care" locations? I've seen things like vector with -999999 coordinates - but that's a nasty trick that might cause bugs (eg. on huge maps).
I had a similar problem in the past, I found there was two solutions. The first solution was to create a wrapper for the class.
Class Vector3Wrapper
{
Vector3 vector;
}
Then I would simply set the Vector3Wrapper to null then if it wasnt null access its vector. A better method would be making it a Nullable type. A example is below.
http://unitypatterns.com/nullable-types/
public class Character : MonoBehaviour
{
//Notice the added "?"
Vector3? targetPosition;
void MoveTowardsTargetPosition()
{
//First, check if the variable has been assigned a value
if (targetPosition.HasValue)
{
//move towards targetPosition.Value
}
else
{
//targetPosition.Value is invalid! Don't use it!
}
}
I have created two scripts one is scrpt_Enemy and other is scrpt_GameManager.
I want to call SubtractLive() function (exist on scrpt_GameManager) from scrpt_Enemy. But it gives error I don't know why. The error is 'SubtractLive' is not a member of 'UnityEngine.Component'.
scrpt_Enemy:
var gameManager : GameObject;
gameManager.GetComponent("scrpt_GameManager").SubtractLive();
scrpt_GameManager:
var lives : int =3;
function SubtractLive(){
lives -= lives;
}
Problem
You were telling Unity that gameManager was of type GameObject. Therefore Unity was freaking out because it doesn't have SubtractLive() under GameObject.
Solution
You can get the correct reference like this:
Create the variable then tell Unity what the type it is.
var gameManager : scrpt_GameManager;
Find the GameObject which has the script attach to it.
gameManager = GameObject.Find("GameManagerGameObject").GetComponent("scrpt_GameManager");
Note that "GameManagerGameObject" is the name of the GameManager in the scene.
Now that you have the reference to the script just call the function like this:
gameManager.SubtractLive();
I am currently playing around in Unity trying to make/test a 2D game. I keep getting the following error when I attempt to access CharacterMotor.playerx from inside camerafollow.js:
An instance of type "CharacterMotor" is required to access non static member "playerx"
Here are my two scripts:
camerafollow.js
#pragma strict
function Start () {
transform.position.x = CharacterMotor.playerx;
}
CharacterMotor.js
#pragma strict
#pragma implicit
#pragma downcast
public var playerx : float = transform.position.x;
You could change playerx to static, but I don't think that's what you want to do (there's probably only one player object, but this would prevent you from ever having multiple CharacterMotors). I think you want/need to retrieve the instance of CharacterMotor that is attached to this gameObject.
#pragma strict
function Start () {
var charMotor : CharacterMotor = gameObject.GetComponent(CharacterMotor);
transform.position.x = charMotor.playerx;
}
An instance of type "CharacterMotor" is required to access non static member "playerx"
The above error message describes precisely what is happening. You are just trying to access a variable without first creating an instance of it. Keep in mind that UnityScript != JavaScript.
To fix this issue, simply change
public var playerx : float = transform.position.x;
to
public static var playerx : float = transform.position.x;
Though this fixes your immediate problem I do not recommend continuing down this path. I suggest that you learn other aspects of the language first (such as classes) so that you can better organize and construct your data.
See: http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)
CharacterMotor is the type, there can be multiple instantiations of your type in memory at the same time so when you call the type name you are not referencing any instance in memory.
to get an instance of the type that is connected to you current gameobject try this:
var charactorMotor : CharacterMotor = gameObject.getComponent("CharacterMotor");
Now you have access to that instances properties
transform.position.x = characterMotor.playerx;
I wanna declare a record inside a class as follows:
class player (x, y)=
object(self)
type gun = {x:int; y:int; active:bool}
val guns = Array.create 5 {x=0; y=0; active=false}
....
but the compiler claim that this line is syntax error : type gun = {x:in ....
when declared outside the class like this
type : gun = {x:int; y:int; active:bool}
class player (x, y)=
object(self)
val guns = Array.create 5 {x=0; y=0; active=false}
....
the error is : unbound value gun.
so anyone know how to reach the same functionality with another way?
thank you!
********* solved***
Bizare now it's working when the type is declared outside, thank you
Why don't you define the type gun outside of the class definition?