Hi! anyone there? I want to call a script of another object and use it’s float number ‘side’. I called that script’s side like this - “Player.PlayerController.side” and it’s sawing me an error. Like this-
You can see what’s the error. If you can’t so I’m writting that error here-
The namespace ‘’ already contains a definition for ‘PlayerMisile’
And here’s my code,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMissile : MonoBehaviour
{
public GameObject Player = null;
public GameObject self;
public Vector3 StartingPoint;
void Start()
{
Player = GameObject.Find(“player”);
if (Player.PlayerController.side == 0)
{
StartingPoint = new Vector3(-1.3f, 0.3f, 2f);
}
if (Player.PlayerController.side == 1)
{
StartingPoint = new Vector3(1.3f, 0.3f, 2f);
}
transform.position = StartingPoint;
transform.eulerAngles = new Vector3(90, 0, 0);
}
Please help me to erase this error if you can!
You may have duplicated the script. That’s OK, but you need to rename the new script file to something different AND rename the class name in the script to match exactly. Never use spaces in script filenames because you can’t use spaces in class names. (If I’m wrong on this please advise!)
Oh, yes, you are right, I accidently created same two scripts. Thanks about that, as I not expert in English yet, I didn’t understood the hard English error sentence properly, sorry for that. Thank you for explaining that error to me. Also thanks you for your code!
Yes, I didn’t thought about that, I accidently created two scripts. I didn’t changed the new script, I just deleted it, because same scripts, same things will happen. I just want to use one of them, you know nobody needs same two scripts. Thanks to you too for responding to help me!
if (Input.GetKeyDown("m"))
{
if (missiles > 0)
{
Instantiate(playerMissile, transform.position, Quaternion.identity);
missiles -= 1;
if (side == "left")
{
side = "right";
}
if (side == "right")
{
side = "left";
}
}
}
Hey, here, another code is not working. My string side is not changing with this code.
Any problem with this code? I saw in the inspector, even if I press ‘M’, playerMissile is created but the string ‘side’ not yet changed. That’s still written left! Please help me with this if you can do.
In other game engines I used similar algorithm where I don’t need to use else, I just use if in similar functions like unity’s void Start(). I think unity applies two times the things written inside of Start() function. So my side is getting changed two times. I didn’t thought ‘if (side == right)’ is alright.
Thanks for your help again.
Will elif be a problem to use? I’m not quit sure so I’m asking it to you.
I wrote it didn’t worked. What I want is if side = “left” so side will be “right”. Then if side is "right’ so it’ll be "left again. I’m now thinking how to do that in Unity. Very hard for me!
Here’s my next full code. Yet a problem is not making the MRchangable when a key released. So the next progress is not yet working. Can you help me to know why after also releasing my ‘m’ key, my bool MRchangable is not getting true?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject playerFire = null;
public GameObject playerMissile = null;
public float movementSpeed = 1f;
public float horizontalInput;
public float verticalInput;
public float health = 10;
public float missiles = 7;
public float bombs = 3;
public bool MChangable = false;
public bool MLchangable = true;
public bool MRchangable = false;
public string side = "left";
void Update()
{
var pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -38f, 38f);
pos.z = Mathf.Clamp(pos.z,-3.5f, 44.8f);
transform.position = pos;
health = Mathf.Clamp(health, 0, 10);
missiles = Mathf.Clamp(missiles, 0, 10);
bombs = Mathf.Clamp(bombs, 0, 10);
if (Input.GetKeyDown("m"))
{
if (missiles > 0)
{
Instantiate(playerMissile, transform.position, Quaternion.identity);
missiles -= 1;
if (MLchangable = true & side == "left")
{
MLchangable = false;
}
if (MRchangable = true & side == "right")
{
MRchangable = false;
}
}
}
if (Input.GetKeyUp("m"))
{
if (MLchangable = false & side == "left")
{
MRchangable = true;
side = "right";
}
if (MRchangable = false & side == "right")
{
MLchangable = true;
side = "left";
}
}
if (Input.GetKeyDown("space"))
{
Instantiate(playerFire, transform.position, Quaternion.identity);
}
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
transform.Translate(horizontalInput * movementSpeed, 0f, verticalInput * movementSpeed);
}
}
Your problem is simply using the wrong operators all over the place. Several times I see MRchangable = false which is the assignment operator. You are setting MRchangable to false with this code. You need to use MRchangable == false to check equality.
Unity is not running Start twice. The code simply executes from top to bottom. So it runs the first “if”, changes the value, then it runs the second “if” and changes it back. “else” simply means “only run this if you didn’t run the ‘if’”.
My code got longer. I think after making this game I’ll have out of 100 lines of codes. I coded more then 100 lines before but in other engines. Looks ugly right?
Now I want to thank you very much, after writing double ‘=’ symbols in the if statements, it’s working! Sorry that your code with else didn’t worked. Even if I used many engine before, I can’t yet adjusted with Unity properly yet because I didn’t learned C# programming basics in school or anywhere. I’m directly learning C# from unity tutorials.
I actually used Unity in 2017, but in middle I hated unity for 3 years and stoped learning and using Unity engine for a few reasons. But just some months ago I was successfully able to fix those reasons and started to feel the power of unity! So I again felt in love of Unity. If I would learn Unity those 3 years, I would be an expert game developer this long. But as I’m not, I need to often ask the problems I found or confusions sometimes in Unity community to get answer. I really appreciate the help of answer givers, because I really feel great when they help me to solve a problem to help me to get one more step to develop my dream game. As you’re also a answer giver and helping me to understand this engine, I really thanked you, to help me to learn my dream game development. Thanks to be this long!